2

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) {
...
}
}
  • Possible duplicate of [Check if all checkboxes are selected](https://stackoverflow.com/questions/5541387/check-if-all-checkboxes-are-selected) – Nawin Nov 19 '19 at 10:33
  • `I currently can see which checkboxes have been checked` - where are you doing that in that code? I can't see any form submission and check of the request variables. – Constantin Groß Nov 19 '19 at 10:34
  • @Nawin this question is not tagged with JavaScript or jQuery... :) – Constantin Groß Nov 19 '19 at 10:34
  • Did you try to check if $_POST['devices'] is empty? if (empty($_POST['devices'] ) { //not all check boxes is check } – Muamar Ali Nov 19 '19 at 10:39
  • @ConstantinGroß I just updated my question. I added the other code. –  Nov 19 '19 at 10:58
  • @Nawin it doesnt really answer my question. –  Nov 19 '19 at 10:58
  • I dont know who marked this as a duplicate when I have not tagged this question with JavaScript nor jQuery! Stackoverflow used to be better! –  Nov 19 '19 at 11:00
  • Unless you tell PHP what the available checkboxes are, there's **no** way for it to know whether they're all checked or not - especially considering unchecked boxes aren't even included in the HTTP request. – CD001 Nov 19 '19 at 11:10
  • @CD001 alright, how can I do that? –  Nov 19 '19 at 11:37
  • Depends on how strict you want to be (e.g. you *could* whitelist the POST vars in the code) - but it looks like you've got an answer that works for you. – CD001 Nov 19 '19 at 11:51

1 Answers1

0
<?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.

Bala Chandar
  • 140
  • 7
  • I like the approach but num_rows() is not working, it shows an error. –  Nov 19 '19 at 11:34
  • That you need handle @bmm, You can get the number rows from the Message class – Bala Chandar Nov 19 '19 at 11:34
  • Message is not a class, its the query fetched from DB. –  Nov 19 '19 at 11:36
  • Then you can get the total number of rows from that message query @bmm – Bala Chandar Nov 19 '19 at 11:37
  • Fatal error: Uncaught Error: Call to undefined method mysqli_result::num_rows() –  Nov 19 '19 at 11:39
  • Just try with **$message->num_rows** – Bala Chandar Nov 19 '19 at 11:43
  • thats not a funciton so it should be like this : if ($message->num_rows >0) {...} –  Nov 19 '19 at 11:46
  • So you got the number of rows from $message->num_rows, Right. – Bala Chandar Nov 19 '19 at 11:49
  • btw how can you save an array in Session and count the items in it? –  Nov 19 '19 at 11:52
  • When the condition trues, you can set the session variable $_SESSION['key_value'][] = $count. Hope this is what you are expecting , If not please explain the thing clearly. – Bala Chandar Nov 19 '19 at 11:54
  • if I set the $_SESSION['key_value'][] = $array_values; and then access it somewhere else, should I just call it like this: $_SESSION['key_value'] ? –  Nov 19 '19 at 12:00
  • actually its showing an errror saying the array values are not string –  Nov 19 '19 at 12:02
  • @bmm I don't know, what you wanted to do with the session.! post your exact problem with code. – Bala Chandar Nov 19 '19 at 12:07
  • nvm that, I am comparing these two if they are the same length like this but doesnt seem to give me a proper return: if ($message->num_rows == $selected_items) { echo "The selected number is equal to the fetched devices"; } –  Nov 19 '19 at 12:08
  • print this **$_SESSION['countable_array']** and post it here Bro. – Bala Chandar Nov 19 '19 at 12:11
  • $selected_items contains all the selected devices. –  Nov 19 '19 at 12:12
  • If you don't mind. I don't understand your problem, Sorry. If possible create a new question and post your queries over there. – Bala Chandar Nov 19 '19 at 12:17
  • How do you set an array in Session? And how would i compare it with the message->num_rows ? –  Nov 19 '19 at 12:31
  • $selected_items = array('5', '4', '12'); // Countable array $_SESSION['countable_array'] = $selected_items; // Initiate the session here print_r($_SESSION['countable_array']) you will get below result $_SESSION['countable_array'][0] = 5 $_SESSION['countable_array'][1] = 4 $_SESSION['countable_array'][2] = 12 So you can check the number of rows like this foreach ($_SESSION['countable_array'] as $key => $value) { # code... if($value == $message->num_rows){ echo "Condition true, Your code goes here..!"; } } – Bala Chandar Nov 19 '19 at 12:41
  • Copy and past above comment in you text editor and test the result. – Bala Chandar Nov 19 '19 at 12:41
  • its not working, it says the length are not the same but in fact they are. –  Nov 19 '19 at 13:05