Search code examples
phpajaxcodeignitercodeigniter-3codeigniter-query-builder

change userlevel on button click in codeigniter


I am trying to update userlevel when click on button. i have a page where it will display all users there i have change level button.by default every user will be in normal level(means 3) so when i click on a button it has to change to 5(lead ) and if i click again it has to change to 9(admin)

here is my view page code:

  <div class="col-md-4">
                                    <form method="post" class="change_userlevel" action="" onclick="getConfirmation(9);">
                                        <input type="hidden" name="id" value="<?php echo $user->id; ?>">
                                        <button type="submit"   class="widget-icon" name="login"><span class='icon-arrow-up'></span></button>
                                    </form>
                                      </div>
<td>
                                    <?php
                                    if($usertype==9)
                                    {
                                      echo 'Admin';
                                    }
                                    elseif($usertype==5)
                                    {
                                        echo 'Teamlead';
                                    }
                                    else
                                     {
                                       echo 'Normal';  
                                     }
                                    ?>
                                 </td>

Here is my script code with ajax:

    <script type='text/javascript'>
function getConfirmation(id){
$.ajax({
        url: "User/change_status",
        type: "post",
        data: id,
        success: function (response) {
         location.reload();              

        }
    });
}
</script>

Here is my controller code method change_status:

   function  change_status(){
$id = $this->input->post('id');
$status = $this->db->query("select userlevel from users where id=9")->row()->userlevel;
if($status==3){
    $status = 5;
} else {
    $status = 9;
}
$data=array('userlevel'=>$status);
$this->db->where('id','id');
$this->db->update('users',$data);
}

I dont know where i have done mistake it is not working.

can anyone help me how to solve this.

Thanks in advance.


Solution

  • Hope this will help you :

    Your ajax code should be like this :

    <script type='text/javascript'>
      function getConfirmation(id)
      {
        if (id != '')
        {
            $.ajax({
              url: "<?=site_url('User/change_status');?>",
              type: "post",
              data: {'id' : id},
              success: function (response) {
                alert('success');
               //location.reload();              
              }
          });
        }
    
    }
    </script>
    

    Your method change_status should be like this :

    function  change_status()
    {
        $id = $this->input->post('id');
    
        $status = $this->db->select('userlevel')->where('id', $id)->get('users')->row()->userlevel;
        if($status == 3)
        {
            $status = 5;
        } else 
        {
            $status = 9;
        }
        $data = array('userlevel' => $status);
        $this->db->where('id', $id);
        $this->db->update('users',$data);
        echo TRUE;exit;
    }