Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve data from multiple multi select dropdown with same name in one form

Tags:

html

php

I am trying to handle multiple multi select boxes/dropdown with same name on one page and on one form. They have the same name because these dropdown/boxes are adding dynamically. My question is how to retrieve data of each select box individually. Its showing the result like this:

Array(
        [0] => M
        [1] => T
        [2] => W 
 )

But I want the result like this

  Array
  (
     [0] => Array(
               [0] => M
               [2] => T
             )
     [1] => Array(
               [0] => W
             )
  )

I am trying this code:

  <td class="v-align-middle semi-bold sorting_1">
                            <div class="form-group form-group-default form-group-default-select2 full-width days" id="111">
                                    <label>SELECT DAYS</label>
                                    <select id="dw" name="days[]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
                                        <option value="M">Monday</option>
                                        <option value="T">Tuesday</option>
                                        <option value="W">Wednesday</option>
                                        <option value="Th">Thursday</option>
                                        <option value="F">Friday</option>
                                        <option value="Sa">Saturday</option>
                                        <option value="Su">Sunday</option>
                                    </select>

                            </div> <br/>
                        </td>
                        <td>

This is the code I am using to clone my table:

   $('.add-ins').on('click', function(){
     var selfId = $(this).attr('id');
     var tId = $(this).parent().parent().find('table').attr('id');
     var lastId = $('.timings#'+tId+' tbody tr:visible:last').attr('id');
     lastId++;
     $('select').select2('destroy'); /* destroy select2 from select tag and then clone it */          
    var clonerow = $('.timings#'+tId+' tbody tr:visible:last').clone(true, true).attr('id', lastId);
    clonerow.appendTo('.timings#'+tId+' tbody');
    $('select').select2(); /* enable Select2 on the select elements */
});

PHP Code:

  $days = $_POST['days'];
  foreach($days as $value){
       echo $value; 
       foreach($value as $week){ 
     echo  $week; 
     }
  }

Or simply this:

   print_r($days);

Kindly, help me in this regard. If there is any other solution like using different name for each select box then kindly tell me how to post them using php $_POST as I can not figure out how many multiselect boxes will be added by the user.

Your suggestions will be highly appreciated. Thanks in advance.

Kind Regards

like image 789
Aisha Avatar asked Dec 14 '25 15:12

Aisha


2 Answers

please code in html like

<select id="dw" name="days[0][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
 <option value="M">Monday</option>
 <option value="T">Tuesday</option>
 <option value="W">Wednesday</option>
 <option value="Th">Thursday</option>
 <option value="F">Friday</option>
 <option value="Sa">Saturday</option>
 <option value="Su">Sunday</option>
</select>

<select id="dw" name="days[1][]" class="full-width select2-offscreen" data-init-plugin="select2" multiple="" tabindex="-1">
 <option value="M">Monday</option>
 <option value="T">Tuesday</option>
 <option value="W">Wednesday</option>
 <option value="Th">Thursday</option>
 <option value="F">Friday</option>
 <option value="Sa">Saturday</option>
 <option value="Su">Sunday</option>
</select>
like image 106
Rakesh Sojitra Avatar answered Dec 16 '25 06:12

Rakesh Sojitra


You should use like following way:

1:
<select id="dw" name="days[0][]" >
    <option value="M">Monday</option>
</select>

2:
<select id="dw" name="days[1][]" >
    <option value="M">Monday</option>
</select>

If you are inserting select box using javascript then take variable to set 0/1/2... for name of select.

POST data:

Array
(
   [0] => Array(
            [0] => M
            [1] => T
          )
   [1] => Array(
            [0] => W
          )
)
like image 26
Parixit Avatar answered Dec 16 '25 04:12

Parixit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!