2

I actually need to delete multiple rows that are checked via ajax in codeigniter. when i click the delete button it gives me the value of all the selected check box but its not passing all the values to controller and hence only the last checked value is getting deletd please help me..

View Page...The javascript is called when the

<div class="multiple_del" title="Delete"><img src="../../images/minus.png"/></div>
                </div>

is clicked

    <table border="0"  cellpadding="0" style="width:100%;margin:0px auto" cellspacing="0" id="product-table">
                    <thead>
                        <tr>
                        <th class="tbl_hdr sorter-false"><img src="../../images/trash.png" style="padding-left:13px;"/></th><!--Multiple Delete--->
                        <th class="tbl_hdr">USER ID</th> <!--ID-->                    
                        <th class="tbl_hdr">‫NAME</th> <!--Name-->                                       
                        <th class="tbl_hdr">EMAIL</th> <!--Email-->
                        <th class="tbl_hdr">PHONE NUMBER‎</th> <!--ph number-->
                        <th class="tbl_hdr sorter-false">STATUS</th> <!--publish/unpublish-->
                        <th class="tbl_hdr sorter-false" colspan="2">ACTION</th> <!--edit/delte-->                                                       
                       </tr>
                       </thead>

                     <tbody>
                       <?php 
                       $count=1;
                       foreach($users as $user){
                           if($count%2==0){?>
                       <tr class="alternate-row"><?php }?>
                       <td><input type="checkbox" name="checkboxlist" value="<?php echo $user->id;?>"/></td>
                       <td><?php echo $user->id;?></td>
                       <td><?php echo $user->username;?></td>
                       <td><?php echo $user->email;?></td>
                       <td><?php echo $user->contact;?></td>
                       <?php if ($user->status==0) 
                       {?>
                       <td>
                       <div align="center">
                       <a class="status unpublished" value="<?php echo $user->id;?>" title="Unpublished"></a>
                       </div>
                       </td>
                       <?php }
                       else 
                       {?>
                       <td>
                       <div align="center">
                       <a class="status published" value="<?php echo $user->id;?>" title="Published"></a>
                       </div>
                       </td>
                       <?php }?>
                       <td>
                       <a class="editbutton"><span class="edit editbutton" title="Edit"><img src="../../images/edit.png"/></span></a>
                       <a class="removebutton" onclick="deleteuser('<?php echo $user->id;?>')"><span class="delete removebutton" title="Delete"><img src="../../images/delete.png"/></span></a>
                       </td>
                       </tr>    
                       <?php $count++;}?> 
                    </tbody>    
                    </table>
<div>
                <div class="multiple_del" title="Delete"><img src="../../images/minus.png"/></div>
                </div>

Javascript...

    $(document).ready(function(){

       $('.multiple_del').click(function(){
                var checkValues = $('input[name=checkboxlist]:checked').map(function()
                {
                    return $(this).val();
                }).get();
                //alert(checkValues);
                var url='<?php echo base_url(); ?>index.php/admin/delete_multiple';

                $.ajax({

                    type: 'POST',
                    url: url,
                    data: { ids: checkValues },
                    success:function(data)
                    {

                          window.location="<?php echo base_url(); ?>index.php/admin/users";
                          $( ".msg1" ).text( "Selected Users Deleted..!!");
                    }
                });

       });





});

Controller...The ajax url goes to this controller

public function delete_multiple()//Delete Multiple Users
      {

        $ids=$this->input->post('ids');
        $this->admin_model->delete_multiple($ids);

      }

Model...

function delete_multiple($ids)//Delete Multiple Users
    {

     $this->db
          ->where_in('id', $ids)
          ->delete('tbl_users');

    }

1 Answers1

0

View:-

<table border='1' width='100%'>
        <thead>
            <th>Username</th>
            <th>Name</th>
            <th>Email</th>
            <th><input type="checkbox" id="checkall" value='1'>&nbsp;<input type="button" id="delete" value='Delete All'></th>
        </thead>

        <tbody>
            <?php 
            foreach($users as $user){
                $id = $user['id'];
                $username = $user['username'];
                $name = $user['name'];
                $email = $user['email'];
                ?>
                <tr id='tr_<?= $id ?>'>
                    <td><?= $username ?></td>
                    <td><?= $name ?></td>
                    <td><?= $email ?></td>
                    <td align='center'><input type="checkbox" class='checkbox' name='delete[]' value='<?= $id ?>' ></td>
                </tr>
                <?php
            }
            ?>
        </tbody>
        

    
    

jQuery / Ajax Code:-

    <!-- Script -->
<script type="text/javascript">
    $(document).ready(function(){

        // Check all
        $("#checkall").change(function(){

            var checked = $(this).is(':checked');
            if(checked){
                $(".checkbox").each(function(){
                    $(this).prop("checked",true);
                });
            }else{
                $(".checkbox").each(function(){
                    $(this).prop("checked",false);
                });
            }
        });

        // Changing state of CheckAll checkbox 
        $(".checkbox").click(function(){
            
            if($(".checkbox").length == $(".checkbox:checked").length) {
                $("#checkall").prop("checked", true);
            } else {
                $("#checkall").prop("checked",false);
            }

        });

        // Delete button clicked
        $('#delete').click(function(){

            // Confirm alert
            var deleteConfirm = confirm("Are you sure?");
            if (deleteConfirm == true) {

                // Get userid from checked checkboxes
                var users_arr = [];
                $(".checkbox:checked").each(function(){
                    var userid = $(this).val();
                    
                    users_arr.push(userid);
                });

                // Array length
                var length = users_arr.length;

                if(length > 0){
                    
                    // AJAX request
                    $.ajax({
                        url: '<?= base_url() ?>index.php/users/deleteUser',
                        type: 'post',
                        data: {user_ids: users_arr},
                        success: function(response){
                            
                            // Remove <tr>
                            $(".checkbox:checked").each(function(){
                                var userid = $(this).val();
                                
                                $('#tr_'+userid).remove();
                            });
                        }
                    });
                }
                else{
                    alert("Please Select At Least one Checkbox");
                }
            }   
            
        });

    });
</script>   

Controller Code:-

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Users extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
        // Load model
        $this->load->model('Main_model');
    }

  
    public function deleteUser(){
        // POST values
        $user_ids = $this->input->post('user_ids');
        // Delete records
        $this->Main_model->deleteUser($user_ids);
        echo 1;
        exit;
    }
}
?>  

Model Code:-

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Main_model extends CI_Model {

  public function __construct() {
    parent::__construct(); 
  }
  
  // Delete record
  public function deleteUser($user_ids = array() ){

    foreach($user_ids as $userid){
        $this->db->delete('users', array('id' => $userid));
    }
    return 1;
  }

}
?>
KUMAR
  • 1,993
  • 2
  • 9
  • 26