0

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>
       </select>
    </div>
</div>

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.

6
  • you are not getting job_category in your PHP script on form submit ? OR select js is not working ? Commented Jul 7, 2016 at 4:19
  • Any JS error showing in your browser console? Commented Jul 7, 2016 at 4:28
  • @AkshayP If I check the posted value using: if(isset($_POST['job_category'])){ die('set'); } the output is set. Commented Jul 7, 2016 at 4:35
  • @woodykiddy no js error found using firebug Commented Jul 7, 2016 at 4:36
  • instead of if(isset($_POST['job_category'])){ die('set'); } try, if(isset($_POST['job_category'])){ print_r($_POST['job_category']); } and tell me the result Commented Jul 7, 2016 at 4:41

1 Answer 1

5

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 :

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

@AkshyaP: It works. In fact I'd tried both $_POST['job_category'] & CI's Input class $this->input->post('jobcategory') to loop through the posted values but they both showed error. Never knew $_REQUEST would work in such circumstances.
which method are you using to submit the form ? POST / GET ?
@AkshyaP: I am using POST all the while.

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.