Question

I'm trying update a tinyint field called inactive from 0 to 1 whenever a button is clicked. I'm sure that I'm getting the correct id so there's no problem there but I can't update the field.

Here is my code for the view.

<a id="deactivate" role="button" href=<?php echo site_url('user/deactivate'); ?>>Deactivate</a>
<script>
$(document).ready(function() {
$('a#deactivate').bind('click',function(event){
      if (confirm("Are you sure you want to deactivate " + id_deactivate + "?")) {
            url_deactivate = ('<?php echo site_url('user/deactivate'); ?>');
                $('a#deactivate').attr('href', function() {
                    var newURL0 =  url_deactivate + "/" + id_deactivate;
                    console.log(newURL0);
                    return newURL0;
                })
          } else{
                event.preventDefault()
          }
    });
});
</script>

for the model:

public function deactivateUser($id, $data){
        if($this->db->update('user', "`inactive` = '".$data."'", "`user_id` = '".$id."'")){
            return true;
        }
        return false;
    }

And for the controller:

public function deactivate($id){
    $this->session->set_userdata('page', 'user'); 
    $this->session->set_userdata('action', 'view');
    $data['inactive'] = 1;
    $this->user_model->deactivateUser($id, $data);
    redirect('user');
}

I'm pretty sure that the problem is with the controller but I don't know how to fix it. Can anyone help me? Thank you in advance.

Était-ce utile?

La solution

in model try this

    $this->db->where('user_id', $id);
    $update = $this->db->update('user', $data); 
    if($update) return TRUE;
    return FALSE:

See more info here

Autres conseils

Instead of setting the href value after confirming the JS dialog (really weird way of going about it), just set the href already in the HTML.

Then you launch the JS confirm with

return (confirm("Are you sure you want to deactivate " + id_deactivate + "?")) ? true : false;

If you return true (when the user accepts the confirmation) you let the default action run its course, which is following the link in the href. If the user doesn't accept the confirmation you cancel the default action with a return false, and nothing happens.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top