title: Populating a dropdown based on the selection of another dropdown using Jquery in Codeigniter
body: <p>I am using Codeigntier and I have the following dropdown in my view file which populates a list of subjects. </p>
  
  <pre><code>&lt;?php echo form_dropdown('subject1', $dropdown_subjects,'',
   'class="required" id="subject1"'); ?&gt;
  </code></pre>
  
  <p>Now when any one selects any value from the dropdown above, I want to send the value to my controller using jquery and query in the following table<code>( SELECT teacherid from table3 WHERE subjectid=$subjectid)</code> to get the teacherid so that I can populate the teacherid list in another dropdown select. If any user changes his selection from the first dropdown I want to get the values of the second dropdown changed also </p>
  
  <p>Table Name: table3</p>
  
  <pre><code>subjectid teacherid
    1        1001
    2        1003
  </code></pre>
  
  <p>So the bottom line is I want to populate a dropdown based on another dropdown. I have found couple of tutorials on this topic but I couldn't really understand those(I know I am stupid).</p>
  
  <p>Would you please kindly show me how my view and controller should look like if I want to achieve this?</p>
  
  <p>Thanks :)</p>
  
  <h1>EDit </h1>
  
  <p>Hi, this is how my controller and view file looks like :</p>
  
  <p>My Controller</p>
  
  <pre><code>   $id= $this-&gt;input-&gt;post('subject_id'); //receiving the ajax post from view 
  
     $this-&gt;db-&gt;select('teachername,teacherid');
     $this-&gt;db-&gt;from('subject_teacher');
     $this-&gt;db-&gt;join('teacher', 'teacher.teacherid = subject_teacher.teacherid');
     $this-&gt;db-&gt;where('subjectid',$id);
     $records = $this-&gt;db-&gt;get('');
  
     $data=array();
     $data[''] = 'Select'; 
     foreach ($records-&gt;result() as $row)
      {
          $data[$row-&gt;teacherid] = $row-&gt;teachername;
      }
  
      return ($data); // I need help here... How to send the data as json?
  </code></pre>
  
  <p>My view:</p>
  
  <pre><code>   &lt;script&gt;
             $(function(){
  
                   $("#subject").change(function(){
              $.ajax({
              url: "&lt;?echo base_url();?&gt;mycontroller/function",
              data: {subject_id: $(this).val()},
              type: "post",
              success: function(msg){
             $("#teacher").html(); // I need help here...how do I get the value from controller and append to my another dropdown named teacher?
          })
        })
  
  
          }); // function ends here   
  
       &lt;/script&gt;
  
  
  
  
    &lt;?php echo form_dropdown('subject1', $dropdown_subjects,'',
     'class="required" id="subject1"'); ?&gt;
  
  
  
     &lt;select name="teacher" id="teacher"&gt;
      &lt;option value=""&gt;Select&lt;/option&gt;
    &lt;/select&gt;
  </code></pre>
  
  <p>Please make the necessary changes in my View and Controller for me. </p>
  
  <p>Thanks in Advance :)</p>