im trying to create multiple checkboxes in html using a while loop in php.
while($counter != 5){
?>
<Input type = 'Checkbox' Name = '<?php echo '$foods[$counter]'; ?>' value ="yeeeey">
<?php
echo $foods[$counter];
echo " ";
?>
<input type="text" class="textfield" value="" name="<?php echo $foods_val[$counter]; ?>"
onkeypress="return isNumber(event)" style="width:55px";>
<P>
<?php
$counter++;
}
$counter = 0;
?>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Input order">
</FORM>
just dont mind the text field for now. basically, im trying to create multiple checkboxes with their name and value based on an array.
$foods = array($rw, $mrd, $rd, ... , $bb);
and each of those variables contains a string. i have a code to detect which checkboxes were checked
if(isset($_POST['Submit1'])){
while($counter != 21){
if(isset($_POST['$foods[$counter]'])){
echo "yeeeeeeeeeeeeeeeeeeeeey";
}
$counter++;
}
}
however, when i try checking some boxes, the string "yeeeeeeeeeeeeeeeeeeeeey" is never printed. it seems that $_POST['$foods[$counter]'] is empty even when it is supposed to contain "yeeeeeey". how do you solve this? sorry for crappy formatting, still getting used to it. thanks
small update. all variables inside $foods[$counter] now contains the same string: "food". my problem now is that all checkboxes behaves as if they were checked and contains the same value "yeeey"
echo
just inside theif(isset)
statement; what if it's just inside thewhile($counter)
statement? Basic debugging should precede asking on SO. – Floris Mar 30 '14 at 17:51echo
commands in your script, so you can see the value of variables at every pass through the loop. This usually teaches you a lot about your program, and why things aren't working. Using a name with[]
generates an array of checkboxes; see @IamManish's answer, or see stackoverflow.com/a/14026375/1967396 for a clearer explanation. – Floris Apr 1 '14 at 2:56