Page 1 of 1

GET, POST, Multi-Selects and the PHP nightmare

Posted: Thu Jun 09, 2005 8:40 am
by theauthor
When the HTML of a form has a field name 'choices' and has five checkboxes with that name, each with a different value, like this:

Code: Select all

<form name=&quote;testform&quote; action=&quote;processor.php&quote; method=&quote;post&quote;>
<input type=&quote;checkbox&quote; name=&quote;choices&quote; value=&quote;One&quote; checked=&quote;checked&quote;>One
<input type=&quote;checkbox&quote; name=&quote;choices&quote; value=&quote;Two&quote; checked=&quote;checked&quote;>Two
<input type=&quote;checkbox&quote; name=&quote;choices&quote; value=&quote;Three&quote; checked=&quote;checked&quote;>Three
<input type=&quote;checkbox&quote; name=&quote;choices&quote; value=&quote;Four&quote; checked=&quote;checked&quote;>Four
<input type=&quote;checkbox&quote; name=&quote;choices&quote; value=&quote;Five&quote; checked=&quote;checked&quote;>Five
<input type=&quote;submit&quote; value=&quote;submit&quote; name=&quote;submit&quote;>
</form>
In PHP, without altering the HTML at all (assume that I have no control over the HTML because there will be remote forms already coded by others that submit to this script) if I look at the values through the PHP-approved method (assuming that every checkbox was checked), I see:

Code: Select all

<?php
$choicevalues=$_POST['choices'];
print($choicevalues); //prints 'One'
?>
Where did the rest of my selections disappear to? How can I get at them? This is the question that is plaguing me.

As a minor additional frustration, what if I don't give a durn what action the form used?
Is there no axiomatic way to access the form variable regardless of method (without turning on the globals option)?

Posted: Thu Jun 09, 2005 9:18 am
by phpScott
$_POST['choices'] should came back as an array.
try

Code: Select all

$choicevalues=$_POST['choices'];
print_r($choicevalues);

Posted: Thu Jun 09, 2005 9:59 am
by pickle
phpScott wrote:$_POST['choices'] should came back as an array.
Before that happens, the checkboxes need to be named "choices[]" as opposed to "choices".

Posted: Thu Jun 09, 2005 8:03 pm
by harrison
I agree with pickle. Naming the checkboxes with the same name without '[]' will only overwrite them, resulting to the value of the last checkbox created. So $choicevalues=$_POST['choices']; will have a value of "Five".