Search code examples
ajaxcodeignitercodeigniter-3codeigniter-query-builder

Delete record with out page refresh in codeigniter is not working


Hi all i am trying to delete my record from datatable with out page refresh in codeigniter i have used ajax i don't know where i have done mistake its not deleting the record

Below is the my view:

    <tbody>
                        <?php
                        if (!empty($employees)) {
                            foreach ($employees as $emp) {
                                 ?>

                                <tr>
                                    <td><?php echo $emp->emp_name; ?></td>
                                    <td><?php echo $emp->salary; ?></td>
                                    <td class='text-center'>
                                    <button type="submit" class="btn btn-info btn-xs confirmation" name="login"><i class='fas fa-edit'></i></button>
                                    </td>

                                    <td class='text-center'>
                                     <button type="submit"  onClick="return ConfirmDelete()" class="btn btn-danger btn-xs confirmation empdelete" id="<?php echo $emp->id;?>"><i class='fas fa-times'></i></button>
                                     </td>
                               </tr>

                                <?php
                            }
                        }
                        ?>
                    </tbody>
    <script>
$(document).ready(function(){
$(".empdelete").click(function(e){
    alert();
   e.preventDefault(); 
    $.ajax({
      alert();
      type: "POST",
      url: "<?=site_url('Employee/delete');?>",
      cache: false,
      data: {id:$(this).attr("id")}, // since, you need to delete post of particular id
      success: function(data) {
         if (data){
            alert("Success");
         } else {
             alert("ERROR");
         }
         return false;

       }
   });
});
});
</script>

Here is the my controller:

  function delete()  
  {
    $id = $this->input->post('id'); // get the post data
    $empdelete=$this->Emp_model->delete($id);
    if($empdelete){
        echo true;
    } else {
        echo false;
    }
 }

Here is my model's method delete:

function delete($id)
{ 
$sql  = "DELETE FROM employees WHERE id=?";
return $this->db->query($sql,array($id));

}

Can any one help me how can i do that with out page refresh i want to delete my record.

Thanks in advance.


Solution

  • Try this:

    $(document).ready(function () {
            function ConfirmDelete() {
                var x = confirm("Are you sure you want to delete?");
                if (x)
                    return true;
                else
                    return false;
            }
            $(".empdelete").click(function (e) {
                var obj = $(this);
                e.preventDefault();
                //alert(); what's this do?
                if (ConfirmDelete() == false) {
                    return false;
                }
                $.ajax({
                    //alert(); this can't go here
                    type: "POST",
                    url: "<?php echo site_url('Employee/delete'); ?>",
                    cache: false,
                    data: {id: $(this).attr("id")},
                    success: function (data) {
                        console.log('ajax returned: ');
                        console.log(data);
                        if (data) {
                            obj.closest('tr').remove();
                            alert("Success");
                        } else {
                            alert("ERROR");
                        }
                        return false;
                    }
                });
            });
        });
    

    and remove HTML onClick:

    <button type="submit" class="btn btn-danger btn-xs confirmation empdelete" id="<?php echo $emp->id;?>"><i class='fas fa-times'></i></button>