Forum Moderators: open
I have a simple form with a javascript function which selects all the elements in the select tags. If I select more than one option from the select the PHP post only returns the last selected value. I've tried adding square brackets to the select name which will post the selected values to PHP but breaks the javascript select all function.
I'm not the worlds greatest javascript coder, all rather a black at to me, so any ideas will be greatly appreciated.
[code]
<?
print_r($_POST);
?>
<form action="<?=$_SERVER['PHP_SELF']?>" name="lookup" method="post">
<select name="s" size="4" multiple>
<option value="one">One
<option value="two">Two
<option value="three">Three
<option value="four">Four
</select>
<input type="button" value="select all" onclick="selAll(true)">
<input type="button" value="select none" onclick="selAll(false)">
<input type="submit">
</form>
<script>
function selAll(_v) {
for(var i=0;i<document.lookup.s.length;i++)
document.lookup.s.options[i].selected=_v;
}
</script>
{/code]
Cheers
Richard
Welcome to WebmasterWorld!
There is actually a bit of a hack to get this to work. I need to append square brackets to the end of the field name, like this:
<select name="s[]" size="4" multiple>
This will make PHP treat it as an Array, and you should be able to loop through all the selected values.
Chad