mysociety / jquery-multi-select

Converts <select multiple> elements into dropdown menus with checkboxes
https://www.mysociety.org/
Other
24 stars 18 forks source link

Doesn't work with POST, only GET #18

Closed TriangleDigital closed 5 years ago

TriangleDigital commented 5 years ago

I found that submitting the form with GET gives all the selections in the query string as expected. However, when substituting POST for the submission method there is only one of the multiple selections, the final one, in the POST array received by the server. To demonstrate this see the attached file ( change index.txt to index.php) Is this a bug or is this plugin designed to work only with GET submission?

index.txt

dracos commented 5 years ago

The plugin works fine with POST, and there is no bug. It is up to the server how it deals with multiple values received. In PHP's case, you should change your select name to end with [] (so inventors[]) and then PHP knows how to handle it fine, putting all the results in an array. See the PHP documentation at https://www.php.net/manual/en/language.variables.external.php

You should look at $_GET, not the unparsed query string, which you would then see is giving you the same results as $_POST. You can see the script submits all data fine using your browser dev console and the Network tab.

So if you change your debug output to e.g. print_r($_POST); and print_r($GET); and change to <select id="modal-example" name="inventors[]" multiple> you'll see it all working fine.

Hope that's helpful.