I am trying to fetch values from all (checked and unchecked) boxes in a form. I know that unchecked values are normally not being sent to the server. I've searched and found this:
<form action="<?php echo $_SERVER[PHP_SELF] ?>" method="post">
<input type="checkbox" id="1" name="box[]" value="1" />
<input type="checkbox" id="2" name="box[]" value="2" />
<input type="checkbox" id="3" name="box[]" value="3" />
<input type="submit" value="Submit" />
</form>
<?php
// $box = $_POST['box'];
$foo = array();
for ($i = 1; $i <= 3; $i++) {
$foo[$i] = in_array($i, $_POST['box']) ? 'on' : 'off';
}
print_r ($foo);
?>
The output is:
Array ( [1] => off [2] => on [3] => off )
It's a pretty interesting solution but it only gets on(1) for checked and off(0) for unchecked boxes, while I want to get the number values from the form. As an output I'd like to have an array with checked and unchecked values. How can I do it preferably using the code above as a backbone? Please help.
I want to elaborate my question. If I have a folowing form:
<form action="" method="post">
<input type = "checkbox" id = "254" name = "voc2[]" value= "8089" />yes
<input type = "checkbox" id = "255" name = "voc2[]" value= "8148" />no
<input type = "checkbox" id = "256" name = "voc2[]" value= "8207" />maybe
<input name="submit" type="submit" />
</form>
Can I get one of the values (say 8089) on the same page if it is not checked? I've tryed:
<?php
if (isset($_POST['submit'])) {
$voc2 = $_POST['voc2'];
echo '<br> $voc2 = '. $voc2[0];
}
?>
but it still shows only the checked values.