3

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

4
  • Please show your HTML code so that we can check Commented Mar 8, 2016 at 6:10
  • I have added the html in my question, kindly check it. Commented Mar 8, 2016 at 6:13
  • If dynamicly added, can you show the code that generates it? Commented Mar 8, 2016 at 6:17
  • Yes, I have added jquery code that clone the table. Kindly, check it. Commented Mar 8, 2016 at 6:21

2 Answers 2

6

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>
Sign up to request clarification or add additional context in comments.

1 Comment

yes, it works. That's great. You saved my day. Thanks alot.
1

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
          )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.