【问题标题】:How can I check if all checkboxes have been checked in PHP?如何检查是否在 PHP 中检查了所有复选框?
【发布时间】:2019-11-19 19:24:38
【问题描述】:

我正在实现一个项目列表,每个项目都有一个复选框。我目前可以看到哪些复选框已被选中,但我想要做的是检查它们是否都已被选中。我该如何实现?

这是我的代码:

<form action="" method="post">
     <?php
                echo "<table>
                    <tr>
                    <th>Customer ID</th>
                    <th>Report ID</th>
                    <th>Report message</th>
                    <th>Device</th>
                    <th>Device no.</th>
                    <th>Barcode</th>
                    <th>IMEI</th>
                    <th>Sale-date</th>
                    </tr>";

                while ($row2 = $clientUsername->fetch_assoc()) {

                    $_SESSION['cl_username'] = $row2["username"];
                    while ($row = $message->fetch_assoc()) {

                        $_SESSION['accept'] = $row["acceptance"];
                        $_SESSION['client_comment'] = $row["message"];
                        $_SESSION['name'] = $row["name"];
                        $_SESSION['sales_date'] = $row["sales_date"];
                        $_SESSION['date_sent'] = $row["date_sent"];


                        $_SESSION['countable_array']  = $row;

                        ?>

                <?php if ($row['acceptance'] == 3) {

                            echo "<tr> <td>
                                  " . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
                            </td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
                            echo "</table>";
                        }
                    }
                }
</form>

    if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['rejected'])) {
 if (count($count_devices) == 1) {
...
}
}

【问题讨论】:

  • I currently can see which checkboxes have been checked - 您在该代码中的哪个位置执行此操作?我看不到任何表单提交和请求变量的检查。
  • @Nawin 这个问题没有用 JavaScript 或 jQuery 标记... :)
  • 您是否尝试检查 $_POST['devices'] 是否为空? if (empty($_POST['devices'] ) { //不是所有的复选框都是选中的 }
  • @ConstantinGroß 我刚刚更新了我的问题。我添加了其他代码。

标签: php html checkbox


【解决方案1】:
<?php
    while ($row2 = $clientUsername->fetch_assoc()) {

        $_SESSION['cl_username'] = $row2["username"];

        $i = 0; // initiate the variable here
        $total_number_of_rows = $message->num_rows(); // Get total number of rows from your object
        while ($row = $message->fetch_assoc()) {

            $_SESSION['accept'] = $row["acceptance"];
            $_SESSION['client_comment'] = $row["message"];
            $_SESSION['name'] = $row["name"];
            $_SESSION['sales_date'] = $row["sales_date"];
            $_SESSION['date_sent'] = $row["date_sent"];


            $_SESSION['countable_array']  = $row;

             if ($row['acceptance'] == 3) {
                $i++; // When conditions trues, increment the variable.
                echo "<tr> <td>
                      " . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
                </td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
                echo "</table>";
            }
        }

        if($i == $total_number_of_rows){ // Here implement this condition, If both equal then all inputs have checked.
            echo "Check box checked all inputs"; 
        }
    } 
?>

我认为您的期望与上述相同。我们需要检查具有增量变量值的总行数。

请在代码部分查看我的评论,以便您理解术语。

【讨论】:

  • 我喜欢这种方法,但 num_rows() 不起作用,它显示错误。
  • 你需要处理@bmm,你可以从Message类中获取行数
  • 消息不是一个类,它是从数据库中获取的查询。
  • 然后你可以从那个消息查询@bmm中得到总行数
  • 致命错误:未捕获错误:调用未定义方法 mysqli_result::num_rows()
猜你喜欢
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 2013-12-25
  • 2014-09-14
  • 2019-10-24
  • 2015-03-13
  • 2011-01-13
相关资源
最近更新 更多