【问题标题】:Issue getting checkbox fields in datatables php在数据表 php 中获取复选框字段的问题
【发布时间】:2018-03-27 09:50:46
【问题描述】:

我正在使用PHP 处理数据表。

我在第一列有一个表格,有一个复选框,上面写着Select All,每一行都有一个复选框。

当用户点击全选复选框时,只有那些可见行的复选框值将被获取并在点击提交后显示在 php 中。

问题是,当我从第 2 页选择一个复选框时,说我已经检查了 ID 为 13 的澳大利亚,然后我转到第 3 页并没有选择任何内容。然后点击提交我没有得到$_POST字段名称与澳大利亚。

但是当检查澳大利亚并单击提交而不更改页面时,它将获取我的$_POST 详细信息。

我想知道当我从不同页面选择多个复选框时,他们将复选框数据获取到 php 的一种方式。

我正在使用最新的数据表 js 文件。

图片用于让开发者理解。

下面是我的 HTML 代码

view.php

<

?php
require 'conn.php';

$sql = "SELECT * FROM tbl_country";
$result = $conn->query($sql);

$country_array = array();

if ($result->num_rows > 0) {
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        $country_array[] = $row;
    }
} else {
    echo "0 results";
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
    </head>
    <body>
        <br>
        <br>

        <div class="container">
            <form action="process.php" method="post">
                <input type="submit" name="submit" value="submit">
                <table class="table table-bordered" id="example" >
                    <thead>
                        <tr>
                            <th>
                                <label for="select_all">
                                    <input type="checkbox" id="select_all" onclick="selectAll()" name="select_all" value="1" > Select All
                                </label>
                            </th>
                            <th>id</th>
                            <th>name</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($country_array as $value) {
                            ?>
                            <tr>
                                <td>
                                    <label for="select_one_<?php echo $value['country_id']; ?>">
                                        <input type="checkbox" class="common_class" id="select_one_<?php echo $value['country_id']; ?>" name="select_one_<?php echo $value['country_id']; ?>" value="select_one_<?php echo $value['country_id']; ?>">
                                    </label>
                                </td>
                                <td><?php echo $value['country_id']; ?></td>
                                <td><?php echo $value['country_name']; ?></td>
                            </tr>
                            <?php
                        }
                        ?>
                    </tbody>
                </table>
            </form>
        </div>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
        </script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js">
        </script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js">
        </script>
        <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js">
        </script>

        <script>
            $(document).ready(function () {
                $('#example').DataTable({
                    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
                });
            });

            function selectAll() {
                var is_checked = document.getElementById('select_all').checked;

                if (is_checked) {
                    $('.common_class').prop('checked', true);
                } else {
                    $('.common_class').prop('checked', false);
                }

                console.log(is_checked);
            }
        </script>
    </body>
</html>

PHP FILE process.php 代码

    <?php
    echo '<pre>';
    print_r($_POST);
?>

php文件conn.php

    <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tempco_cms";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

提前致谢

【问题讨论】:

标签: php jquery datatable datatables datatables-1.10


【解决方案1】:

使用 serializeArray 和 ajax 提交所有复选框(选中/未选中)

var frmData = $("#my_form").serializeArray(); //my_form the id of your form
$(".common_class").each(function(){
 frmData.push({name: "country_selection[]", value: $(this).val()}); 
});

$.ajax({
 url: "your_action_url",
 type: 'POST',
 data: frmData,
 cache: false,
 success: function(result) {  
 },
 error: function(result) {
  }
 }); 

这将发布您的所有表单字段和一个名为 country_selection[] 的新对象数组

在你的 php 中你现在可以使用它来迭代它

$checkbox = $_POST['common_selection'];
for($i=0;$i<count($checkbox);$i++){
echo $checkbox[$i];
}

【讨论】:

    【解决方案2】:
    $('#deleteTriger').on("click", function(event){ // triggering delete one by one
            if( $('.deleteRow:checked').length > 0 ){  // at-least one checkbox checked
                var ids = [];
                $('.deleteRow').each(function(){
                    if($(this).is(':checked')) {
                        ids.push($(this).val());
                    }
                });
                var ids_string = ids.toString();  // array to string conversion
                console.log(ids_string);
                $.ajax({
                    type: "POST",
                    url: "/getIds.php",
                    data: {data_ids:ids_string},
                    success: function(result) {
                        table.draw(); // redrawing datatable
                    },
                    async:false
                });
            }
        });
    

    并在 php 文件中获取所有选定的 ID,如 $data_ids = $_POST['data_ids'];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多