0

I am trying to populate two dropdown menu whose value is dependent with other one. In total I have 3 dropdowns. one for selecting a class and other two for selecting the subjects and exams of selected class. I can populate one drop down menu (say for eg: select subject). but how can i populate both of them. My code is as follows:

 <table>
 <tr>
 <td> select class</td>
 </tr>
 <tr>
 <td> select subject</td>
 </tr>
 <tr>
 <td> select exam</td>
 </tr>
 <tr>
 <td><select class="form-control m-bot15" name="class_id" value='' onchange="myFunction(this)" style="float:left;" id="carId">
 <option value="">select a class</option>
 <option value="1">One</option>
 <option value="2">Two</option>
 <option value="3">Three</option>
 </select></td>
 </tr>
 <tr>
 <td><select class="form-control m-bot15" name="subject_id" value='' style="float:left;" id="emptyDropdown">
 <option value="">select a subject</option>
 </select></td>
 <tr>
 </tr>
 <td><select class="form-control m-bot15" name="exam_id" value='' style="float:left;" id="emptyDropdown2">
 <option value="">select a subject</option>
 </select></td>
 </tr>
 </table>

This is my view function My script for populating subject name is :

 <script>
 function myFunction(obj)
 {
 $('#emptyDropdown').empty()
 var dropDown = document.getElementById("carId");
 var carId = dropDown.options[dropDown.selectedIndex].value;
 $.ajax({
 type: "POST",
 dataType: 'json',
 url: "<?php echo base_url();?>mark/newdrp",
 data: { 'carId': carId },
 success: function(data){
 $.each(data, function () {
 $('#emptyDropdown').append('<option value='+this.subject_id+'>' + this.name + '</option>');
 });
 }
 });
 }
 </script>

My controller function newdrp is

 public function newdrp(){
 $classId = $this->input->post('carId');
 $subjects = $this->subject_model->Subjectbyclassid($classId);
 echo json_encode($exams);
 }

This works fine. Iam getting the subjects list in my dropdown. But i want to pass one more json object like this

 public function newdrp(){
 $classId = $this->input->post('carId');
 $subjects = $this->subject_model->Subjectbyclassid($classId);
 $exams = $this->exam_model->Exambyclassid($classId);
 echo json_encode(array($subjects,$exams));
 }

This is my console preview

 [[{"subject_id":"1","name":"Physics","class_id":"1","teacher_id":null},{"subject_id":"2","name":"Chemistry","class_id":"1","teacher_id":null},{"subject_id":"3","name":"Mathematics","class_id":"1","teacher_id":null}],[{"exam_id":"22","name":"BW9","date":"03\/10\/16","class_id":"1","comment":""},{"exam_id":"26","name":"BW10","date":"17\/10\/16","class_id":"1","comment":""},{"exam_id":"30","name":"BW11","date":"31\/10\/16","class_id":"1","comment":""},{"exam_id":"34","name":"BW12","date":"14\/11\/16","class_id":"1","comment":""},{"exam_id":"40","name":"BW13","date":"28\/11\/16","class_id":"1","comment":""},{"exam_id":"45","name":"BW14","date":"11\/12\/16","class_id":"1","comment":""},{"exam_id":"46","name":"Revision Exam 1","date":"02\/01\/17","class_id":"1","comment":""},{"exam_id":"49","name":"Revision Exam 2","date":"8\/01\/2017","class_id":"1","comment":""},{"exam_id":"51","name":"Revision Exam 3","date":"15\/01\/17","class_id":"1","comment":""},{"exam_id":"55","name":"Revision Exam 4","date":"22\/01\/17","class_id":"1","comment":""},{"exam_id":"57","name":"Revision exam 5","date":"26\/01\/2017","class_id":"1","comment":""},{"exam_id":"59","name":"Revision Exam 6","date":"29\/01\/17","class_id":"1","comment":""}]]

How can i loop through this an display the exam name in corresponding dropdown. Please help

asked Apr 15, 2017 at 6:15

1 Answer 1

1

You'll have to loop through the second array as well, data being the parent array

success: function(data){
 var examArr = data[1];
 $.each(examArr, function () {
 $('#emptyDropdown2').append("<option value='" + $(this).exam_id + "'>" + $(this).name + "</option>");
 });
 }
answered Apr 15, 2017 at 6:32
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help !

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.