【问题标题】:multiple delete in codeigniter using ajax使用ajax在codeigniter中多次删除
【发布时间】:2016-04-05 10:03:09
【问题描述】:

我实际上需要删除在 codeigniter 中通过 ajax 检查的多行。 当我单击删除按钮时,它给了我所有选中复选框的值,但它没有将所有值传递给控制器​​,因此只有最后一个选中的值被删除,请帮助我..

查看页面...当

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

被点击

    <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..!!");
                    }
                });

       });





});

控制器...ajax url 转到这个控制器

public function delete_multiple()//Delete Multiple Users
      {

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

      }

型号...

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

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

    }

【问题讨论】:

  • 将您的复选框名称更改为此..name="checkboxlist[]"
  • 这能解决我的问题吗?
  • 更改复选框名称不会调用 javascript...它没有给我警报框中所选复选框的值
  • 你需要根据name="checkboxlist[]"来调整你的javascript。我建议复习问题 - stackoverflow.com/questions/9493531/…
  • 感谢@Kamal,上述问题在很大程度上有所帮助..

标签: php ajax codeigniter


【解决方案1】:

查看:-

<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 代码:-

    <!-- 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>   

控制器代码:-

<?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;
    }
}
?>  

型号代码:-

<?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;
  }

}
?>

【讨论】:

    猜你喜欢
    • 2019-02-07
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2013-04-14
    • 2015-01-04
    相关资源
    最近更新 更多