简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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