Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting selected values from a multiple select form in Laravel

For generating a drop-down list with an item selected by default, the following is done:

echo Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), 'S');

So I generated a drop-down list that has more than one item selected by default, in the following way:

echo Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));

But how do I get the more than one selected values?

Input::get('size') returns only the last selected string.

like image 282
SUB0DH Avatar asked Mar 12 '13 10:03

SUB0DH


People also ask

How show multiple selected values in dropdown in PHP?

Get Multiple Selected Values of Select Dropdown in PHPAdd the multiple tag with select tag also define array with name property. Make sure the Fruits array is not empty, run a foreach loop to iterate over every value of the select dropdown. Display the selected values else show the error message to the user.

How do you use multiselect?

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


2 Answers

First, if you want to have multiple item selected by default, you have to give an array of values as 3rd parameter, not a simple value.

Exemple:

Form::select('size', array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), array('S', 'M'), array('multiple'));

should show the select with S and M selected.

For the second point, you should try to give a name like size[] instead of size, it could be solve the problem (because your posted select is not a simple value, its an array of values)

like image 186
MatRt Avatar answered Sep 28 '22 00:09

MatRt


Usual Select statements go

<select name="select_name" id="select_name" multiple="multiple">

And the workflow is that Laravel gets the form elements by their name. To make it work, change the name to an array.

<select name="select_name[]" id="select_name" multiple="multiple">

This will make laravel get the values of select as an array of data.

like image 29
Farveaz Avatar answered Sep 27 '22 23:09

Farveaz