Hanthony Tagam
Hanthony Tagam

Reputation: 111

Codeigniter deleting process with sweetalert wont delete the data

i coud'nt spot the error. pls help i have used dataTable and inside is my delete button

$list = $this->foobar->get_datatables();
    $data = array();
    $userId = $_POST['start'];

    foreach ($list as $foobar) {
       $userId++;
       $row = array();

       $row[] = $foobar->userId;
       $row[] = $foobar->UserType;
       $row[] = $foobar->FirstName." ".$foobar->LastName;
       $row[] = $foobar->EmailAddress;
       $row[] = $foobar->Position;
       $row[] = $foobar->BranchId;

       $row[] = '<td class="w3-row" >
                <form method="get" class="w3-half " action="edit_user/'.$foobar->userId.'">
                    <button class="btn btn-default" title="View">
                    <i class="fa fa-eye"></i></button></form>

                    <button class="btn btn-danger" href="javascript:void()" title="Delete" onclick="delete_user('.$foobar->userId.')">
                    <i class="fa fa-trash" ></i></button>
                    </td>';
       $data[] = $row;
    }
  $output = array(
     "draw" => $_POST['draw'],
     "recordsTotal" => $this->foobar->count_all(),
     "recordsFiltered" => $this->foobar->count_filtered(),
     "data" => $data,     
    );
    //output to json format
    echo json_encode($output);

now the onclick will find the function in my javascript such as delete_user with the userdata
`

function delete_user(userId) {
         swal({
            title: "Are you sure?",
            text: "You will not be able to recover this user account!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel plx!",
            closeOnConfirm: false,
            closeOnCancel: false
         },
         function(isConfirm) {
            if (isConfirm) {
               swal("Deleted!", "user account has been deleted.", "success");
               //ajax delete data to database
               $.ajax({
                  url: "<?php echo site_url('/Admin_Dashboard/delete_user/')?>" + userId,
                  type: "POST",
                  dataType: "JSON",
                  success: function(data) {
                     table.ajax.reload(null, false); //just reload table
                 },
                  error: function(jqXHR, textStatus, errorThrown) {
                     alert('Error deleting data');
                 }
               });
            } else {
               swal("Cancelled", "user account is safe :)", "error");
            }
         });
      }  `                                                                             

and the ajax will search in my controller as Admin_Dashboard/delete_user

 `function delete_user()
{
    $this->User->delete_by_id($id);
}`

and lastly send call my delete model

public function delete_by_id($userId)
{
    $this->db->where('userId', $userId);
    $this->db->delete($this->table);
}

Upvotes: 1

Views: 1352

Answers (1)

B. Desai
B. Desai

Reputation: 16436

You need to change your function in controller to receive parameters

function delete_user($id) //<---------- Add $id
{
    $this->User->delete_by_id($id);
}

Upvotes: 1

Related Questions