Edit the Logged In Profile with Ajax
<p>I'm new to CodeIgniter and need some help :). 
I managed to build a CRUD functions with ajax over some tutorials and I can Edit, Add new, Delete all user shown on my page. What I want now is when i Login with a user I just want the Logged in Profile to be Edited, not others. Please if someone can help me how can I manage to do that.  Thank You in Advance.</p>

<p>Employee.php Controller</p>

<pre><code>&lt;?php

defined('BASEPATH') OR exit('No direct sripct access allowed');

Class Employee extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this-&gt;load-&gt;model('Employee_m', 'm');
    }


function index()
{
    $this-&gt;load-&gt;view('employee/index');

}

public function showAllEmployee()
{
    $result = $this-&gt;m-&gt;showAllEmployee();
    echo json_encode($result);
}

public function add_user()
{
    $result = $this-&gt;m-&gt;add_user();
    $msg['success'] = false;
    $msg['type'] = 'add';
    if ($result) {
        $msg['success'] = true;
    }
    echo json_encode($msg);
}

public function edit_user()
{
    $result = $this-&gt;m-&gt;edit_user();
    echo json_encode($result);
}

public function update_user()
{
    $result = $this-&gt;m-&gt;update_user();
    $msg['success'] = false;
    $msg['type'] = 'update';
    if ($result) {
        $msg['success'] = true;
    }
    echo json_encode($msg);
}

public function delete_user()
{
    $result = $this-&gt;m-&gt;delete_user();
    $msg['success'] = false;
    if ($result) {
        $msg['success'] = true;
    }
    echo json_encode($msg);
}
</code></pre>

<p>Employee_m Model</p>

<pre><code>&lt;?php defined('BASEPATH') OR exit('No direct sripct access allowed');
class Employee_m extends CI_Model
{

public function showAllEmployee()
{

    $query = $this-&gt;db-&gt;get('users');
    if ($query-&gt;num_rows() &gt; 0){
        return $query-&gt;result();
    }
    else
    {
        return false;
    }
}


public function add_user()
{
    $field = array(
        'firstname' =&gt; $this-&gt;input-&gt;post('txtFirstName'),
        'lastname' =&gt; $this-&gt;input-&gt;post('txtLastName'),
        'username' =&gt; $this-&gt;input-&gt;post('txtUsername'),
        'user_email' =&gt; $this-&gt;input-&gt;post('txtUserEmail'),
        'user_password' =&gt; $this-&gt;input-&gt;post('txtUserPassword')
    );
    $this-&gt;db-&gt;insert('users',$field);
    if ($this-&gt;db-&gt;affected_rows() &gt; 0){
        return true;
    }else{
        return false;
    }
}

public function get_id(){
    $id = $this-&gt;input-&gt;get('id');
    $this-&gt;db-&gt;where('id',$id);
    $query = $this-&gt;db-&gt;get('users');
    if ($query-&gt;num_rows() &gt; 0){
        return $query-&gt;result();
    }
    else
    {
        return false;
    }
}


public function update_user()
{
    $id = $this-&gt;input-&gt;post('txtId');
    $field = array(
        'firstname' =&gt; $this-&gt;input-&gt;post('txtFirstName'),
        'lastname' =&gt; $this-&gt;input-&gt;post('txtLastName'),
        'username' =&gt; $this-&gt;input-&gt;post('txtUsername'),
        'user_email' =&gt; $this-&gt;input-&gt;post('txtUserEmail'),
        'user_password' =&gt; $this-&gt;input-&gt;post('txtUserPassword')
    );
    $this-&gt;db-&gt;where('id',$id);
    $this-&gt;db-&gt;update('users',$field);
    if ($this-&gt;db-&gt;affected_rows() &gt; 0){
        return true;
    }else{
        return false;
    }
}

function delete_user()
{
    $id = $this-&gt;input-&gt;get('id');
    $this-&gt;db-&gt;where('id',$id);
    $this-&gt;db-&gt;delete('users');
    if ($this-&gt;db-&gt;affected_rows() &gt; 0){
        return true;
    }else{
        return false;
    }
}

}
</code></pre>

<p>Index View</p>

<pre><code>&lt;?php $this-&gt;load-&gt;view('components/page_head'); ?&gt;

&lt;?php
if (isset($this-&gt;session-&gt;userdata['logged_in'])) {
    $username = ($this-&gt;session-&gt;userdata['logged_in']['username']);
    $id = ($this-&gt;session-&gt;userdata['logged_in']['id']);

} else {
    header("location: user_authentication");
}
?&gt;

&lt;div class="col-sm-9"&gt;
    &lt;div class="alert alert-success" style="display: none;"&gt;

    &lt;/div&gt;
&lt;button id="btnAdd" class="btn btn-success"&gt;Add New&lt;/button&gt;
&lt;table class="table table-bordered table-responsive" style="margin-top: 20px;"&gt;
    &lt;thead&gt;
    &lt;tr&gt;
        &lt;td&gt;ID&lt;/td&gt;
        &lt;td&gt;First Name&lt;/td&gt;
        &lt;td&gt;Last Name&lt;/td&gt;
        &lt;td&gt;Username&lt;/td&gt;
        &lt;td&gt;e-Mail&lt;/td&gt;
        &lt;td&gt;Actions&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody id="showdata"&gt;

    &lt;/tbody&gt;
&lt;/table&gt;

&lt;div id="myModal" class="modal fade" tabindex="-1" role="dialog"&gt;
    &lt;div class="modal-dialog" role="document"&gt;
        &lt;div class="modal-content"&gt;
            &lt;div class="modal-header"&gt;
                &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                &lt;h4 class="modal-title"&gt;Modal title&lt;/h4&gt;
            &lt;/div&gt;
            &lt;div class="modal-body"&gt;
                &lt;form id="myForm" action="" method="post" class="form-horizontal"&gt;
                    &lt;input type="hidden" name="txtId" value="0"&gt;
                    &lt;div class="form-group"&gt;
                        &lt;label class="label-control col-md-4"&gt;First Name&lt;/label&gt;
                        &lt;div class="col-md-6"&gt;
                            &lt;input type="text" name="txtFirstName" class="form-control"&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="form-group"&gt;
                        &lt;label class="label-control col-md-4"&gt;Last Name&lt;/label&gt;
                        &lt;div class="col-md-6"&gt;
                            &lt;input type="text" name="txtLastName" class="form-control"&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;div class="form-group"&gt;
                        &lt;label class="label-control col-md-4"&gt;Username&lt;/label&gt;
                        &lt;div class="col-md-6"&gt;
                            &lt;input type="text" name="txtUsername" class="form-control"&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;

                    &lt;div class="form-group"&gt;
                        &lt;label class="label-control col-md-4"&gt;e-Mail&lt;/label&gt;
                        &lt;div class="col-md-6"&gt;
                            &lt;input type="email" name="txtUserEmail" class="form-control"&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;

                    &lt;div class="form-group"&gt;
                        &lt;label class="label-control col-md-4"&gt;Password&lt;/label&gt;
                        &lt;div class="col-md-6"&gt;
                            &lt;input type="password" name="txtUserPassword" class="form-control" placeholder="******"&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/form&gt;
            &lt;/div&gt;
            &lt;div class="modal-footer"&gt;
                &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                &lt;button type="button" id="btnSave" class="btn btn-primary"&gt;Save changes&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;&lt;!-- /.modal-content --&gt;
    &lt;/div&gt;&lt;!-- /.modal-dialog --&gt;
&lt;/div&gt;&lt;!-- /.modal --&gt;

&lt;div id="deleteModal" class="modal fade" tabindex="-1" role="dialog"&gt;
    &lt;div class="modal-dialog" role="document"&gt;
        &lt;div class="modal-content"&gt;
            &lt;div class="modal-header"&gt;
                &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                &lt;h4 class="modal-title"&gt;Confirm Delete&lt;/h4&gt;
            &lt;/div&gt;
            &lt;div class="modal-body"&gt;
                Do you want to delete this record?
            &lt;/div&gt;
            &lt;div class="modal-footer"&gt;
                &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                &lt;button type="button" id="btnDelete" class="btn btn-danger"&gt;Delete&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;&lt;!-- /.modal-content --&gt;
    &lt;/div&gt;&lt;!-- /.modal-dialog --&gt;
&lt;/div&gt;&lt;!-- /.modal --&gt;

&lt;script&gt;
    $(function () {
        showAllEmployee();

        // Add New
        $('#btnAdd').click(function () {
            $('#myModal').modal('show');
            $('#myModal').find('.modal-title').text('Add new user');
            $('#myForm').attr('action','&lt;?php echo base_url() ?&gt;employee/add_user');
        });

        $('#btnSave').click(function () {
//            alert('test');
            var url = $('#myForm').attr('action');
            var data = $('#myForm').serialize();

            // validate the form
            var firstname = $('input[name=txtFirstName]');
            var lastname = $('input[name=txtLastName]');
            var username = $('input[name=txtUsername]');
            var user_email = $('input[name=txtUserEmail]');
            var user_password = $('input[name=txtUserPassword]');
            var result = '';
            if (firstname.val()==''){
                firstname.parent().parent().addClass('has-error');
            }else {
                firstname.parent().parent().removeClass('has-error');
                result +='1';
            }
            if (lastname.val()==''){
                lastname.parent().parent().addClass('has-error');
            }else {
                lastname.parent().parent().removeClass('has-error');
                result +='2';
            }
            if (username.val()==''){
                username.parent().parent().addClass('has-error');
            }else {
                username.parent().parent().removeClass('has-error');
                result +='3';
            }
            if (user_email.val()==''){
                user_email.parent().parent().addClass('has-error');
            }else {
                user_email.parent().parent().removeClass('has-error');
                result +='4';
            }
            if (user_password.val()==''){
                user_password.parent().parent().addClass('has-error');
            }else {
                user_password.parent().parent().removeClass('has-error');
                result +='5';
            }


            if(result == '12345'){
                $.ajax({
                    type: 'ajax',
                    method: 'post',
                    url: url,
                    data: data,
                    async: false,
                    dataType: 'json',
                    success: function (response) {
                        if (response.success){
                            $('#myModal').modal('hide');
                            $('#myForm')[0].reset();
                            if(response.type=='add'){
                                var type = 'added'
                            }else if(response.type=='update'){
                                var type ="updated"
                            }
                            $('.alert-success').html('User '+type+' successfully').fadeIn().delay(4000).fadeOut('slow');
                            showAllEmployee();
                        }else{
                            alert('Error');
                        }
                    },
                    error: function () {
                        alert('Could not add Data ');
                    }
                });
            }
        });

        //edit
        $('#showdata').on('click', '.item-edit', function() {

            var id = $(this).attr('data');
            $('#myModal').modal('show');
            $('#myModal').find('.modal-title').text('edit user');
            $('#myForm').attr('action','&lt;?php echo base_url() ?&gt;employee/update_user');
            $.ajax({
                type: 'ajax',
                method: 'get',
                url: '&lt;?php echo base_url() ?&gt;employee/edit_user',
                data: {id: id},
                async: false,
                dataType: 'json',
                success: function(data) {
                    $('input[name=txtFirstName]').val(data.firstname);
                    $('input[name=txtLastName]').val(data.lastname);
                    $('input[name=txtUsername]').val(data.username);
                    $('input[name=txtUserEmail]').val(data.user_email);
                    $('input[name=txtUserPassword]').val(data.user_password);
                    $('input[name=txtId]').val(data.id);
                },
                error: function() {
                    alert('Could not Edit Data');
                }
            });
        });

        //delete
        $('#showdata').on('click', '.item-delete', function () {
            var id = $(this).attr('data');
            $('#deleteModal').modal('show');
            $('#btnDelete').unbind().click(function () {
                $.ajax({
                    type: 'ajax',
                    method: 'get',
                    async: false,
                    url: '&lt;?php echo base_url() ?&gt;employee/delete_user',
                    data: {id: id},
                    dataType: 'json',
                    success: function (response) {
                        if(response.success){
                            $('#deleteModal').modal('hide');
                            $('.alert-success').html('User deleted successfully').fadeIn().delay(4000).fadeOut('slow');
                            showAllEmployee();
                        }else{
                            alert('Error');
                        }
                    },
                    error: function () {
                        alert('Error deleting');
                    }
                });
            });
        });

        //function
        function showAllEmployee() {
            $.ajax({
                type: 'ajax',
                url: '&lt;?php echo base_url() ?&gt;employee/showAllEmployee',
                async: false,
                dataType: 'json',
                success: function (data) {
                    var html = '';
                    var i;
                    for (i = 0; i &lt; data.length; i++) {
                        html += '&lt;tr&gt;' +
                            '&lt;td&gt;'+data[i].id+'&lt;/td&gt;'+
                            '&lt;td&gt;' + data[i].firstname + '&lt;/td&gt;' +
                            '&lt;td&gt;' + data[i].lastname + '&lt;/td&gt;' +
                            '&lt;td&gt;' + data[i].username + '&lt;/td&gt;' +
                            '&lt;td&gt;' + data[i].user_email + '&lt;/td&gt;' +
                            '&lt;td&gt;' +
                            '&lt;a href="javascript:;" class="btn btn-info item-edit" data="'+data[i].id+'"&gt;Edit&lt;/a&gt;' +
                            '&lt;a href="javascript:;" class="btn btn-danger item-delete" data="'+data[i].id+'"&gt;Delete&lt;/a&gt;' +
                            '&lt;/td&gt;' +
                            '&lt;/tr&gt;';
                    }
                    $('#showdata').html(html);
                },
                error: function () {
                    alert('Could not get Data from Database');
                }
            });
        }
    });
&lt;/script&gt;
&lt;/div&gt;
&lt;div class="col-sm-3"&gt;
    &lt;?php
    echo "Hello &lt;b id='welcome'&gt;&lt;i&gt;" . $username . "&lt;/i&gt; !&lt;/b&gt;";
    echo "&lt;br/&gt;";
    echo "Your ID is " . $id;
    echo "&lt;br/&gt;";
    ?&gt;
    &lt;a href="&lt;?php echo base_url() ?&gt;user_authentication/logout"&gt;Logout&lt;/a&gt;
&lt;/div&gt;
&lt;?php $this-&gt;load-&gt;view('components/page_tail'); ?&gt;
</code></pre>