Click to See Complete Forum and Search --> : checkbox problems...


Da Warriah
08-28-2003, 03:37 PM
*sigh* why cant forms and PHP just get along and play nicely?

alright, ive got a number of checkboxes numbered like so:

<input type="checkbox" name="edit" value="1" />
<input type="checkbox" name="edit" value="2" />
<input type="checkbox" name="edit" value="3" />
...

what id like to be able to do is let the user choose any number of checkboxes, and then submit the info to another page, which will use those numbers in a bunch of stuff...but i did a simple test on the second page (edit.php):

<?php
if(!empty($edit)){
print "Not Empty: ".$edit;
} else {
print "Empty: ".$edit;
}
?>

and heres what i got:

Not Empty: 2

even if i had checkboxes 1 and 2 selected, id only get 2...and if i chose checkbox 3 plus 1, 2, or no others, id get 3...is the variable overwriting itself or something? how do it get, for example, 1, 2, and 3? or would i have to name each checkbox separately, like:

name="1" value="1"

help would be appreciated;)

pyro
08-28-2003, 06:34 PM
Agreed that this is a bit of a problem. You will need to name them with a [] at the end, like this:

<input type="checkbox" name="edit[]" value="1" />
<input type="checkbox" name="edit[]" value="2" />
<input type="checkbox" name="edit[]" value="3" />

Why is this a problem? Because it is not valid... :(

Anyway, what this does is makes an array of all the checkboxes, so you can loop through and get the values that were checked.

Da Warriah
08-28-2003, 07:07 PM
oh jeez...*smacks head* and to think...i used the EXACT same technique just this morning to use multiple file uploads...jeez, im such an idiot...lol...i didnt even THINK about using the same thing, lol...

so id use something like $_POST['edit'][0] to get the first variable, right? thanks for the help pyro...:D

pyro
08-28-2003, 08:17 PM
Yes, $_POST['edit'][0] will return the first variable that they checked. So, if they checked the second and third ones, $_POST['edit'][0] will return "2"...