简体   繁体   English

如何检查 PHP 中的所有复选框是否已选中?

[英]How can I check if all checkboxes have been checked in PHP?

I am implementing a list of items and each has a checkbox.我正在实现一个项目列表,每个项目都有一个复选框。 I currently can see which checkboxes have been checked but what I want to do is check if all of them have been checked.我目前可以看到哪些复选框已被选中,但我想要做的是检查它们是否都已被选中。 How can I implement that?我该如何实施?

Here is my code:这是我的代码:

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

I think you expecting the same as above.我认为您期望与上述相同。 We need to check the Total number of rows with Incremental variable value.我们需要检查具有增量变量值的总行数。

Please review my comment inside the code part, so that you can understand terms.请查看我在代码部分中的评论,以便您理解术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM