Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one handle multiple value variables? 1

Status
Not open for further replies.

jimoblak

Instructor
Joined
Oct 23, 2001
Messages
3,620
Location
US
I have a situation where it would be nice to have a list box in a form where multiple options can be selected:

Code:
<form name=&quot;FruitBasket&quot; method=&quot;get&quot;>
<select name=&quot;FRUITS&quot; size=&quot;4&quot; multiple>
<option value=&quot;Apple&quot;>Apple</option>
<option value=&quot;Banana&quot;>Banana</option>
<option value=&quot;Orange&quot;>Orange</option>
</select><input type=&quot;submit&quot; value=&quot;Submit&quot;>
</form>

When the form is submitted, I get the following:

Code:
[URL unfurl="true"]http://localhost/multichoice.php?FRUITS=Apple&FRUITS=Orange[/URL]

How do I push this info into an array in PHP? I can only get the last value ('orange') - - (as it replaces the first value read).
 
I nearly always use POST for forms, so I don't know how this will work with a GET-method form, but PHP has an undocumented feature in how it deals with certain form element names.

If you change the name of your select from &quot;FRUITS&quot; to &quot;FRUITS[]&quot; (like an array with no index reference), then inside PHP, $_GET['FRUITS'] will itself be an array containing all the selected values.

This works for any element type, not just SELECTs

Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top