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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Handle Multiple choices from a Select Box in PHP?

FORMS

How to Handle Multiple choices from a Select Box in PHP?

by  vacunita  Posted    (Edited  )
PHP and Forms: Multiple Selects.

Recently I've seen many questions dealing with multiple select boxes in PHP. So I decided to write a small faq explaining How to use Multple Select Boxes in PHP.
Given the following form:

You must have an understanding of the Basics of arrays, and of form submissions using the POST method to fully understand this FAQ.

Code:
<form action=[green]mypage.php[/green] method=[red]POST[/red]>
<SELECT multiple name="options">
<option value=1>One</option>
<option value=2>Two</option>
<option value=3>Three</option>
<option value=4>Four</option>
<option value=5>Five</option>
</select>
<input type=submit name=Submit value="Send Values">
</form>

This form submits the selected options to [green]mypage.php[/green] to get processed.

If you were to attempt to acces the values in [green]mypage.php[/green] like so:

Code:
echo $_POST['options'];

The display would be

[quote PHP]Array[/quote];

The word "[blue]Array[/blue]" is the only thing that would be output. This would be consistent with the fact that there were several items passed, however if you were to access the variable as an array, as would be logical to do so:

Code:
$myoptions=$_POST['options'];

echo $myoptions[0];
echo $myoptions[1];
echo $myoptions[2];
etc...

The output would be:
[quote PHP]

One
Notice: Uninitialized string offset: 1 in mypage.php on line 37
Notice: Uninitialized string offset: 2 in mypage.php on line 38
etc...
[/quote]

Why does this happen?
This is because of the way the values get sent to PHP.
When the form is submitted the browser sends:

Code:
options=firstchoice,
however since there are more choices it continues to assing values:
options=secondchoice
options=thirdchoice
etc..

This effectively overwrites the previous value that was assigned to $options.

In order to get every option that was selected you need to add a pair of brackets at the end of the Select box's name like so:

Code:
...
<SELECT multiple name="options[red][][/red]">
...

The brackets tell PHP that the submitted data is in actuality an Array.
So instead of overwriting the values that get sent, they get added to subsequent positions in the array.
options[]=1
options[]=2
options[]=3
etc...

This will now allow us to access every value that was selected, from the array.

Code:
$myoptions=$_POST['options'];

Then:

echo $myoptions[0];
echo $myoptions[1];
echo $myoptions[2];
etc...


Would echo out as:

[quote PHP]
1
3
5
etc...
[/quote]

With this in Mind we can use any of the array functions available in PHP to access the Array and do with as we please.

Should we need to find out how many options were selected we could issue a [blue]count()[/blue] function on our array:

Code:
$selecteditems=count($myoptions);

For more info on Arrays, and functions that can be used with them please visit the PHP online manual at: http://www.php.net/manual/en/language.types.array.php
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top