Forum Moderators: coopster
I'm pretty new to PHP/forms...and I've got a pretty dumb problem (i.e. there is probably an easy solution).
Well, I'm unsure of how to describe the problem, so I'll just give an example.
Checkbox: Red
Checkbox: Blue
Checkbox: Yellow
How would I get that information in to PHP (query) without knowing in advance what the names on the checkboxes were? The only solution my programming-retarded brain can come up with is running a loop to try every possible name that the checkbox could have...which, I'm sure, is a great idea *laughs*
If my question doesn't make sense, I apologize. I did my best, but I'll give it another shot if it's confusing.
Thanks so much!
~Charlie
The best way to handle check boxes like this is to name them a special way. Let's say you have this code in your form:
<input type="checkbox" name="color_one" value="red" />
<input type="checkbox" name="color_two" value="yellow" />
<input type="checkbox" name="color_three" value="blue" />
This describes your situation: having multiple checkboxes with different names. Now, how do we get around this? We name the checkboxes so that the values are stored within an array like this:
<input type="checkbox" name="[b]color[][/b]" value="red" />
<input type="checkbox" name="[b]color[][/b]" value="yellow" />
<input type="checkbox" name="[b]color[][/b]" value="blue" />
Now on the action page, add the following to see what I'm talking about:
echo '<pre>';
print_r($_POST['color']);
echo '</pre>';
Now you have an array of the selected boxes. See how that works out for you :)
good luck
Can I use checkboxes like these to do it?
<input type="checkbox" value="Red" name="list[]">
<input type="checkbox" value="Blue" name="list[]">
What would the PHP $_POST method for something like that look like?
$list = $_POST['list[]'];
Perhaps something like that?
And finally...how would extract the values out of the array? Like...how would I know what the keys are unless this was a set script for a specific form? Or does HTML number the array with keys like 1 (red), 2 (blue), etc. when it posts the data?
Haha, I'm loading you guys up with tons of simple questions. Thanks!