Rajan Acharya Rajan Acharya - 1 year ago 310
PHP Question

how to get multiple selected values from multiple select options php codeigniter?

I was trying to implement tag words selection using select2 for my project in codeigniter but I was unable to read or get the value of the the multiselect option I implemented using select2 available at https://github.com/select2/select2.
Here is my HTML.

<div class="form-group">
<label class="col-md-3 control-lable">Select job categories.</label>
<div class="col-md-4">
<select style="width:120%" class="form-control select2" name="job_category" multiple="multiple" data-placeholder="Select job categories" style="width: 100%;">
<option value="3">Civil</option>
<option value="6">Electrical</option>
<option value="15">Javascript</option>
<option value="2">Programming</option>
<option value="4">Networking</option>

I have included all necessary js(jquery.min.js, bootstrap.min.js and select2.full.min.js) & css(bootstrap.min.css & select2.min.css) files which I think are necessary for the select2 option to work properly in my project.

Can anyone help me get through this?
Any help is duly appreciated.

Answer Source

change name="job_category" to name="job_category[]". This will post an array having values of selected options.

and in PHP you can get those values as :

 foreach ($_REQUEST['job_category'] as $selectedOption)
     echo $selectedOption."\n";

When you want to get multiple selected options you should use [] in name attribute.