1

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.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
suresh
  • 439
  • 3
  • 18

1 Answers1

1

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;
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • i have updated your code so when i click on button it is giving success alert but its not updating userlevel value in db – suresh Aug 13 '18 at 09:58
  • pls see my updated answers for that and let me know its working for you – Pradeep Aug 13 '18 at 10:04
  • yeah its working and can you please tell me how to pass id dynamically in to getconfirmation in onclick="getConfirmation(9); – suresh Aug 13 '18 at 10:07
  • u can do something like this : `getConfirmation('=$user->id;?>')` – Pradeep Aug 13 '18 at 10:09
  • is there any possibility to show the userlevel name instaed of success alert for ex: if i click on normal level when it is gogint o change to lead i need to display alert like changing to lead – suresh Aug 13 '18 at 10:11
  • u can echo `userlevel name` instead of `true` in your controller method – Pradeep Aug 13 '18 at 10:22
  • if i echio in controller will it display as alert? – suresh Aug 13 '18 at 10:40
  • yes just try with name or id it will show u as an alert but in ajax do like this : `alert(response);` – Pradeep Aug 13 '18 at 10:54
  • i can put only one one echo right like echo 'teamlead' ;after team lead update i want to change it to admin; – suresh Aug 13 '18 at 11:01