Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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"

share|improve this question
    
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 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. – Floris Apr 1 '14 at 2:56
up vote 1 down vote accepted

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">
share|improve this answer
    
so what will be the value of hobbies[]? new to php sorry – ryuuuuuusei Mar 31 '14 at 0:03
    
$hobbies[0] = yeeeey; $hobbies[1] = yeeeey; $hobbies[2] = yeeeey; $hobbies[3] = yeeeey; $hobbies[4] = yeeeey; – IamManish Mar 31 '14 at 6:30
    
Because every time we are storing same value in checkbox.... – IamManish Mar 31 '14 at 6:32

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 ;)

share|improve this answer
    
already tried but no success, reviewing my code for now – ryuuuuuusei Mar 31 '14 at 0:02
<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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.