使用codeIgniter中的复选框删除多个记录

7dl7o3gd  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(120)

创建项目表

CREATE TABLE IF NOT EXISTS `items` (  
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,  
  `description` text COLLATE utf8_unicode_ci NOT NULL,  
  PRIMARY KEY (`id`)  
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;

然后执行步骤2:在application/config/routes.php中添加路由器并添加以下行

$route['item'] = "item";  
$route['itemDelete']['post'] = "item/deleteAll";

然后执行步骤3:创建项目控制器我们将使用deleteAll()和index()创建这个控制器,为此我们将创建一个item.php文件,然后添加代码,如下所示:

<?php
defined('BASEPATH')OR exit('No direct dcript not allowed');
class Item extends CI_Controller{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    public function index(){
        $data['data']=$this->db->get("items")->result();
        $this->load->view('item',$data);
    }
    public function deleteAll(){
        $ids=$this->input->post('ids');

        $this->db->where_in('id', explode(",", $ids));
        $this->db->delete('items');

        echo json_encode(['success'=>"Item Deleted Successfully."]);
    }
}
?>

然后步骤4:创建一个名为item.php的视图文件视图文件将在application/views/item.php中创建

<!DOCTYPE html>
<html>

<head>
    <title>Delete Multiple Records with checkbox</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-12 margin-tb">
                <div class="pull-left">
                    <h2>CodeIgniter 3 - Delete Multiple records with checkbox</h2>
                </div>
            </div>
        </div>
        <button style="margin-bottom: 10px" class="btn btn-primary delete_all" data-url="/itemDelete">Deleted All Selected</button>
        <table class="table table-bordered" style="margin-top: 20px;">
            <thead>
                <tr>
                    <th width="50px"><input type="checkbox" id="master"></th>
                    <th>Title</th>
                    <th>Description</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($data as $item){?>
                    <tr>
                        <td><input type="checkbox" class="sub_chk" data-id="<?php echo $item->id; ?>"></td>
                        <td><?php echo $item->title; ?></td>
                        <td><?php echo $item->description; ?></td>
                    </tr>
                    <?php } ?>
            </tbody>
        </table>

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

        $('#master').on('click', function(e) {
            if ($(this).is(':checked', true)) {
                $(".sub_chk").prop('checked', true);
            } else {
                $(".sub_chk").prop('checked', false);
            }
        });

        $('.delete_all').on('click', function(e) {

            var allVals = [];
            $(".sub_chk:checked").each(function() {
                allVals.push($(this).attr('data-id'));
            });

            if (allVals.length <= 0) {
                alert("Please select row.");
            } else {

                var check = confirm("Are you sure you want to delete this row?");
                if (check == true) {

                    var join_selected_values = allVals.join(",");

                    $.ajax({
                        url: $(this).data('url'),
                        type: 'POST',
                        data: 'ids=' + join_selected_values,
                        success: function(data) {
                            console.log(data);
                            $(".sub_chk:checked").each(function() {
                                $(this).parents("tr").remove();
                            });
                            alert("Item Deleted successfully.");
                        },
                        error: function(data) {
                            alert(data.responseText);
                        }
                    });

                    $.each(allVals, function(index, value) {
                        $('table tr').filter("[data-row-id='" + value + "']").remove();
                    });
                }
            }
        });
    });
</script>
</body>

</html>

然后错误显示如下:无法加载资源:服务器以状态404(未找到)响应

e5njpo68

e5njpo681#

您不能像CodeIgniter 3中那样定义路由。您需要从itemDelete路由中删除['post']

$route['itemDelete'] = "item/deleteAll";

但是,如果您确实希望将该路由限制为仅POST请求,我想您可以执行如下操作:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $route['itemDelete'] = "item/deleteAll";
}

相关问题