Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple select2 returning only a single selection on form submit

I am using multiple select2 in one of the forms. After submitting a form, in the backend, only the single selected value is fetched whereas multiple options are selected.

HTML

<form id="test" method="POST">
    <select name="dps_days_to[NL]" class="dokan-form-control dps_days_selection" required  multiple="multiple">
        <option></option>
        <option value="0">Sunday</option>
        <option value="1">Monday</option>
        <option value="2">Tuesday</option>
        <option value="3">Wednesday</option>
        <option value="4">Thurday</option>
    </select>
</form>

JS

jQuery(document).ready(function( $ ){
    jQuery('.dps_days_selection').select2({
    placeholder: "Please select a day"
    });
});

PHP

var_dump($_POST['dps_days_to']);

RESULT

Array
(
    [0] => 1
)

For example: if Sunday & Monday are selected from available options in select2 multiple dropdown, only the value of options Monday is returned (i.e, 1)

Please, can someone help me?

like image 759
Himani Avatar asked Nov 01 '25 11:11

Himani


1 Answers

@Himani

If you want to get data as per name dps_days_to[NL], you can make changes as per below. Declare [NL] as Array[] i.e. dps_days_to[NL][] , so it can store multiple values.

If [NL] is fixed everytime, then this works.

<select name="dps_days_to[NL][]" class="dokan-form-control dps_days_selection" required  multiple="multiple">

RESULT

[dps_days_to] => Array
(
    [NL] => Array
    (
        [0] => 1
        [1] => 2
    )
)
like image 75
G_real Avatar answered Nov 03 '25 04:11

G_real