I have the following multi-select box in a HTML form, where user can select one or more option.
If you add a hidden input before the multiple select element, it will send the hidden input value if none of the multiple select items have been selected. As soon as you select an option though, that selected value is used instead.
This way I was able to distinguish 2 different scenarios in Laravel/php, being:
myitems
to empty (requiring the hidden input, so PHP receives an empty string for myitems
)myitems
(so excluding any myitems
input form the form. PHP will not receive the myitems
key)Sample code:
<input type="hidden" name="myitems" value="" />
<select name="myitems[]" multiple>
<option value="1">Foo</option>
<option value="2">Bar</option>
</select>
Include <input type="hidden" name="yourfield" value="" />
to your form where the name is the same as for your multiple="multiple"
select.
It is unfortunate that browsers do not send empty form parameters for multiple="multiple"
selects like they do for non-multiple selects or a normal inputs.
Using a dummy value as proposed in the accepted answer is not a very clean solution.
(This question has nothing to do with jQuery)