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!
+1
A:
<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 />';
}
}
?>
karim79
2009-09-04 06:19:26
Thank you. Man you've helped me a lot. haha
Nate Shoffner
2009-09-04 06:31:33
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?
Anthony
2009-09-04 06:35:40
@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.
karim79
2009-09-04 06:44:52
I actually had to removed the brackets in order for it to work.
Nate Shoffner
2009-09-04 06:45:39
@Nate Shoffner - are you using a multi or normal select?
karim79
2009-09-04 06:49:07
Normal, I changed it.
Nate Shoffner
2009-09-04 06:52:15
A:
Without a button:
<form method="get">
<select multiple="multiple" name="things[]" onchange="this.form.submit()">
...
</select>
</form>
Jani Hartikainen
2009-09-04 06:26:05
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
2009-09-04 06:28:40
erp...<select multiple="multiple" name="things[]" onchange="this.form.submit()"><noscript><input type="submit" /></noscript>
Anthony
2009-09-04 06:29:16
+1
A:
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>
Anthony
2009-09-04 06:33:59