May 6 2008

OK, this is basic PHP stuff really, but it’s something I don’t use very often so I thought I’d add a little note to remind myself.

HTML:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
	<option value="one">one</option>
	<option value="two">two</option>
	<option value="three">three</option>
	<option value="four">four</option>
	<option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

PHP:

<?php
	$test=$_POST['test'];
	if ($test){
	 foreach ($test as $t){echo 'You selected ',$t,'<br />';}
	}
?>

Source: http://onlinetools.org/tricks/using_multiple_select.php

3 Responses to “Multiple Select Boxes in PHP”

  1. Jon says:

    Yeah, it’s quite handy – it works with checkboxes as well – just add in the square brackets to the name and php will treat them as an array:

  2. Jon says:

    Oh, this won’t let me post HTML – let’s try with some alpha entities…

    <input type=”checkbox” name=”something[]” value=”one” />
    <input type=”checkbox” name=”something[]” value=”two” />
    <input type=”checkbox” name=”something[]” value=”three” />

  3. Recent Links Tagged With "onlinetools" - JabberTags says:

    [...] public links >> onlinetools Multiple Select Boxes in PHP Saved by MonsterInTheBasement on Thu 06-11-2008 Has your work been plagiarized? Saved by [...]

Leave a Reply