php form handling multiple select entries

Last Updated on Sunday, 3 January 2010 01:34 Written by Dan Thursday, 14 August 2008 10:18

The problem: You need to use the result of a multiple select form element in your php script. Handling a $_POST request gives only one entry from the form element, not all those selected.
The solution: Use array name in form element name ie multiselect becomes multiselect[] or
<select name=”multipleselect[]“>
<option value=”0″>0</option>
<option value=”1″>1</option>
<option value=”2″>2</option>
<option value=”3″>3</option> , etc
then handle with
foreach($_POST['multiselect'] AS $multi) {
// code to deal with each selected item
}
A version of this was found in Using Arrays with Form Data in PHP in PHP 5 Unleashed by John Coggeshall

Learn More

 

Learn Coding & Web Design at Treehouse

 

domain names

 

Brighter Planet's 350 Challenge

Archives

 

 

Sage UK Ltd

Designed by RocketTheme
WordPress is Free Software released under the GNU/GPL License.