Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post values from a multiple select

How can I post values from a multiple select in a form? When I hit submit none of the selected values are posted.

<form id="form" action="" method="post">     <div>         <select id="inscompSelected" multiple="multiple" class="lstSelected">             <option value="1">1</option>             <option value="2">2</option>             <option value="3">3</option>             <option value="4">4</option>                         </select>         <input type="submit" value="submit">     </div> </form> 
like image 740
user1430949 Avatar asked Jul 23 '12 16:07

user1430949


People also ask

How do I select multiple values from a dropdown in HTML?

For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.


1 Answers

You need to add a name attribute.

Since this is a multiple select, at the HTTP level, the client just sends multiple name/value pairs with the same name, you can observe this yourself if you use a form with method="GET": someurl?something=1&something=2&something=3.

In the case of PHP, Ruby, and some other library/frameworks out there, you would need to add square braces ([]) at the end of the name. The frameworks will parse that string and wil present it in some easy to use format, like an array.

Apart from manually parsing the request there's no language/framework/library-agnostic way of accessing multiple values, because they all have different APIs

For PHP you can use:

<select name="something[]" id="inscompSelected" multiple="multiple" class="lstSelected"> 
like image 68
iambriansreed Avatar answered Oct 03 '22 14:10

iambriansreed