简体   繁体   中英

HTML forms multi-select using GET

NOTE :

It's sort of hard to google for this, because "GET" is bringing up a lot of "how to 'get' the value of a form with javascript," etc.

PROBLEM :

TL;DR version first

Can you use a multi-select from with the GET method of a form and still retrieve each individual value?

extended explanation

The requirements of a searchable catalog I'm building include using the GET method with the form, so that the user can see their results in the url, and also send a direct link to the search results to another customer, etc.

One of the searchable fields is a multi-select box (a select box with the MULTIPLE attribute). My back end is written in PHP, and I usually handle a multi-select by setting the name to an array variable (name="multiselect[]") and the post variable includes an array of the selected options ($_POST["multiselect"] == array()) .

The problem in this case is that the multi-select values are passed to the get string like this:

action?multiselect=1&multiselect=2

So whatever the last value is replaces the value of the first initialization of the variable in the get string. (in the above example, multiselect would equal "2").

Trying to make the name an array just makes the array value replaced in the same manner, like this

action?multiselect[]=1&multiselect[]=2

Will result in $_GET["multiselect"] == 2

I had originally recommended using a checkbox, as it would allow us to name the elements differently and check for true/false on each one, but there are around 30 values for this particular multi-select, and they want it to be in a scroll-able area.

Works for me. Just tried

http://www.nearby.org.uk/tmp/multi-test.html

<select name="multiselect[]" ...

Results in a URL ?multiselect%5B%5D=2&multiselect%5B%5D=3

which will make $_GET['multiselect'] an array in PHP.

Perhaps you have something else in your system, stripping the [] ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM