Dealing with a form select that alows multiple entries

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
LazarusCrusader
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2004 12:57 pm

Dealing with a form select that alows multiple entries

Post by LazarusCrusader »

I have a form with a select field that alows multiple selections. How do I get all of the selected info when the form is posted?
I've tried this but got an error:

Code: Select all

foreach($_POSTї'StateDesired'] as $value) 
{ 
$desired .= $value . ", "; 
}
Any ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Presuming your form contains something like..
<select name="StateDesired[]" size="5" multiple>

then you can use :
if(!empty($_POST['StateDesired'])){
echo join(', ', $_POST['StateDesired']);
}
LazarusCrusader
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2004 12:57 pm

Post by LazarusCrusader »

Getting this error with your code:
Warning: join(): Bad arguments
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Hmm..shouldn't do, if you have name="StateDesired[]" in the form, then it comes in as an array. The if(!empty checks to make sure 'something' was selected, and the join operates on the StateDesired array.

Maybe post a snippet of your code? (including the select form element)
LazarusCrusader
Forum Newbie
Posts: 6
Joined: Thu Jan 15, 2004 12:57 pm

Post by LazarusCrusader »

Got it... had to correct my syntax...

Thanks a bunch!
Post Reply