0

I have 10 checkboxes, like so

<input type="hidden" name="box1" value="0"><input type="checkbox" id="box1" name="box1" value="1">
<input type="hidden" name="box2" value="0"><input type="checkbox" id="box2" name="box2" value="1">
<input type="hidden" name="box3" value="0"><input type="checkbox" id="box3" name="box3" value="1">

and so forth. These checkboxes are all inside a form, and when submitted will go to a PHP script. How am I able to access the values (on or off) for each one. So far I have tried using this:

<input type="hidden" name="box[]" value="0"><input type="checkbox" id="box1" name="box[]" value="1">
<input type="hidden" name="box[]" value="0"><input type="checkbox" id="box2" name="box[]" value="1">
<input type="hidden" name="box[]" value="0"><input type="checkbox" id="box3" name="box[]" value="1">

with

$x = $_POST["box"];
foreach ($x as $y){
    echo $y . "</br>";
}

But this gives random, inaccurate results (say for example box 1, 3 and 4 are clicked, it may give something like this: [0, 0, 0, 1, 1, 0, 0, 1], with 1 being on and 0 off). Can anyone give me a hint in the right direction? Thank you in advance

Lachlan Mather
  • 273
  • 1
  • 5
  • 16

1 Answers1

0

You can check if a checkbox has been checked by using isset($_POST['checkbox_name']) which returns true if it is checked and false if it is not.

For example;

if(isset($_POST['box1'])){
  //do something positive
}

I do not think you need the other hidden fields.

Douglas Hosea
  • 1,002
  • 13
  • 30