×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

FORMS

How to Handle Multiple choices from a Select Box in PHP? by vacunita
Posted: 25 Apr 06 (Edited 13 Jun 12)

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=mypage.php method=POST>
<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 mypage.php to get processed.

If you were to attempt to acces the values in mypage.php like so:

CODE

echo $_POST['options'];

The display would be

Quote (PHP)

Array
;

The word "Array" 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...

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[]">
...

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...

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 count() 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....

Back to PHP FAQ Index
Back to PHP Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close