GET, POST, Multi-Selects and the PHP nightmare

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theauthor
Forum Newbie
Posts: 1
Joined: Thu Jun 09, 2005 8:35 am

GET, POST, Multi-Selects and the PHP nightmare

Post 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)?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

$_POST['choices'] should came back as an array.
try

Code: Select all

$choicevalues=$_POST['choices'];
print_r($choicevalues);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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".
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
harrison
Forum Commoner
Posts: 30
Joined: Thu Jun 09, 2005 12:23 am

Post 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".
Post Reply