1

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 "&nbsp;&nbsp;&nbsp;"; 

 ?>

<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"

ryuuuuuusei
  • 98
  • 5
  • 22
  • Do you find that anything is set? What happens if you put an `echo` just inside the `if(isset)` statement; what if it's just inside the `while($counter)` statement? Basic debugging should precede asking on SO. – Floris Mar 30 '14 at 17:51
  • im still new to php so your "basic debugging" may not be basic for me, sorry – ryuuuuuusei Mar 31 '14 at 00:05
  • What I mean is: you can put lots of `echo` 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 http://stackoverflow.com/a/14026375/1967396 for a clearer explanation. – Floris Apr 01 '14 at 02:56

3 Answers3

1

I think, your problem is at least related to the quotes you're using in $_POST['$foods[$counter]']. If you use "double quotes", PHP variables beginning with $ are replaced by their value, but if you use 'single quotes', they are not. So you're trying to access the key '$foods[$counter]' (literally!) of $_POST.

Replacing them by "double quotes" may work, but since $foods contains strings, you don't need the quotes at all: $_POST[$foods[$counter]]

If that still doesn't work, there probably are some more flaws in your code. For the moment, give this a trie ;)

Callidior
  • 2,899
  • 2
  • 18
  • 28
1

Here is code to generate array of check box through php.


<?php
    $i = 0;
    while($i < 5) {
        echo "<input type='checkbox' name='hobbies[]' />";
        $i++;
    }

This code will create array of 5 checkbox...you don't need to specify counter variable like you did...

<Input type = 'Checkbox' Name = '<?php echo '$foods[$counter]'; ?>' value ="yeeeey">
IamManish
  • 160
  • 1
  • 8
0
<input type="text" class="textfield" value="" name="food_vals[]" onkeypress="return isNumber(event)" style="width:55px";>

Then on the server side:

foreach ($_POST['food_vals'] as $value) {
     echo $value;
}
Fadey
  • 430
  • 2
  • 11