-1

after looking for ages on the internet i thought id ask here for help. I have a simple checkbox table here in which i click the checkbox and the result is posted back in PHP, but when i dont click any checkbox and submit it i get the following error.

Notice: Undefined index: idlights in C:\xampp\htdocs\lt4\checkbox\index.php on line 44

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\lt4\checkbox\index.php on line 44

How can i validate this table so that if nothing is checked it just says something like "Please check a checkbox" or something like this? i have tried

if (empty($camid)

{

echo "<p style='color: red'>Please check a checkbox </p>";
}

But that didn't work, Here is my code, Any help on this would be very much appreciated

<?php
            include('conn.php');
                $query=mysqli_query($conn,"select * from `camera`");
                while($row=mysqli_fetch_array($query)){
                    ?>
                    <tr>
                        <td><input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]"></td>
                        <td><?php echo $row['cameras']; ?></td>


                    </tr>
                    <?php
                }
            ?>

        </tbody>
    </table>
    <br>
    <input type="submit" name="submit" value="Submit">
    </form>
    </div>
    <div>
        <h2>You Booked:</h2>
        <?php
            if (isset($_POST['submit'])){
                foreach ($_POST['camid'] as $id):


                $sq=mysqli_query($conn,"select * from `camera` where camid='$id'");
                $srow=mysqli_fetch_array($sq);
                echo $srow['cameras']. "<br>";

                endforeach;
            }
        ?>
       ```

Double
  • 11
  • 2
  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Alon Eitan Apr 13 '20 at 06:29
  • 1
    You just need an additional if statement. see my answer below – Kasun Rajapaksha Apr 13 '20 at 07:26
  • One more suggestion, if possible edit the question to "How to validate set of checkboxes/ array of checkboxes?" – Kasun Rajapaksha Apr 13 '20 at 07:33
  • Since you helped me of course! its done, thankyou brother. – Double Apr 13 '20 at 07:36

2 Answers2

1

Do you use any framework? Try to add required in you checkbox

<input type="checkbox" value="<?php echo $row['camid']; ?>" name="camid[]" required>
Thomas_krk
  • 214
  • 1
  • 8
  • That did work, but it requires all the boxes to be ticked, what i was after was as long as 1 checkbox was ticked. – Double Apr 13 '20 at 06:58
  • Glad you solved it. FYI if you want only one "choice" to be checked is better to use type="radio" instead of check box. If you want at least one choice to be checked then checkbox is fine. – Thomas_krk Apr 13 '20 at 07:37
  • Thanks! will remember that in future, yeah i just wanted atleast one to be checked but unfortunately it wanted to select all the checkboxes, but thankyou! – Double Apr 13 '20 at 07:38
-1

Just use an additional if statement like below.

<?php

        if (isset($_POST['submit'])){


            if(isset($_POST['camid'])){

               foreach ($_POST['camid'] as $id):


                $sq=mysqli_query($conn,"select * from `camera` where camid='$id'");
                $srow=mysqli_fetch_array($sq);
                echo $srow['cameras']. "<br>";

                endforeach; 
            }

        }
    ?>
Kasun Rajapaksha
  • 536
  • 1
  • 5
  • 22
  • Hey! Thankyou so much, i knew i wasn't to far off, This is very helpful i wont forget to use this in the future! – Double Apr 13 '20 at 07:28