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.
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.
For windows: Hold down the control (ctrl) button to select multiple options. For Mac: Hold down the command button to select multiple options.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With