I have a drop down list that has options that need to be passed through a query string. How would I go about doing this? Also, would anyone be able to list a way to do this both using a button and without using a button? Thank you!
3 Answers
<form method="get">
<select multiple="multiple" name="things[]">
...
</select>
<input type="submit" value="submit"/>
</form>
<?php
if(isset($_GET['things'])) {
foreach($_GET['things'] as $thing) {
echo $thing . '<br />';
}
}
?>
4 Comments
Anthony
I'm still unclear as to what the problem was that this solved. was it the use of the brackets in the name to make it an array?
karim79
@Anthony - he said 'options' so I figured he meant a multi-select and he was wondering how to get the array of selected options to the server, the gotcha being the [] after the select's name attribute, and knowing that turns $_GET['things'] into an array.
NateShoffner
I actually had to removed the brackets in order for it to work.
karim79
@Nate Shoffner - are you using a multi or normal select?
Based on Jani's response, are you wanting to have the form submit without a button but still have a backup button if the user doesn't have javascript? You can use noscript to cover that:
<form action="script.php" method="get">
<div>
<select name="options" onchange="this.form.submit()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<noscript>
<input type="submit" value="Go!" />
</noscript>
</div>
</form>
Comments
Without a button:
<form method="get">
<select multiple="multiple" name="things[]" onchange="this.form.submit()">
...
</select>
</form>
2 Comments
Anthony
To be more accessible and backward compatible, I'd go with:
<select multiple="multiple" name="things[]" onchange="this.form.submit()"><noscript><input type="submit" />Anthony
erp... <select multiple="multiple" name="things[]" onchange="this.form.submit()"><noscript><input type="submit" /></noscript>