简体   繁体   中英

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 (ie, 1)

Please, can someone help me?

@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[] ie 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
    )
)

I am pretty sure,

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

here you are mentioning class attributes multiple times .
merge it to one and once check.

Or you can try giving starting values from 1 instead of 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM