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"

3
  • 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. Commented Mar 30, 2014 at 17:51
  • im still new to php so your "basic debugging" may not be basic for me, sorry Commented Mar 31, 2014 at 0: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 stackoverflow.com/a/14026375/1967396 for a clearer explanation. Commented Apr 1, 2014 at 2:56

3 Answers 3

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">
2
  • so what will be the value of hobbies[]? new to php sorry Commented Mar 31, 2014 at 0:03
  • $hobbies[0] = yeeeey; $hobbies[1] = yeeeey; $hobbies[2] = yeeeey; $hobbies[3] = yeeeey; $hobbies[4] = yeeeey; Commented Mar 31, 2014 at 6:30
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 ;)

0
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.