Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send multiple select boxes values with same name attribute in codeigniter

these are two select boxes with same name can any one guide mehow can I pass two select boxes values as one one single array

<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">
<option value="">-- Choose Service Areas --</option>
<option value="1">all over `enter code here`Andhrapredesh</option>
<option value="11">asia</option>
<option value="12">North-South America</option>
</select>
<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">`enter code here`
 <option value="">-- Choose Service Areas --</option>
 <option value="1">all over Andhrapredesh</option>
 <option value="11">asia</option>
 <option value="12">North-South America</option>
 </select>
like image 835
srikanth Avatar asked May 09 '26 08:05

srikanth


1 Answers

I hope this will help you.

Source code

$(document).on('change', 'select[name="servicetypes[]"]', function(){
  
        var selectedValues = {};
        $('select[name="servicetypes[]"]').each(function(){
            var text = $(this).children("option").filter(":selected").text();
            var value = $(this).val();
            selectedValues[text] = value;

        });

        var textValue = $('#servicetypesArray').val(selectedValues);
     
      alert(JSON.stringify(selectedValues, null, 4));

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">
    <option value="">-- Choose Service Areas --</option>
    <option value="1">all over Andhrapredesh</option>
    <option value="11">asia</option>
    <option value="12">North-South America</option>
    </select>
    <select multiple="multiple" name="servicetypes[]" id="servicetypes" class="form-control">`enter code here`
     <option value="">-- Choose Service Areas --</option>
     <option value="1">all over Andhrapredesh</option>
     <option value="11">asia</option>
     <option value="12">North-South America</option>
     </select>

    <input type="text" id="servicetypesArray" name="data[servicetypes][]" value="" hidden/>

You will have your values in a multidimensional array named 'servicetypes'

Here is a CODEPEN Demo

like image 151
Athul AK Avatar answered May 10 '26 20:05

Athul AK