Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7888745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:54:47+00:00 2026-06-03T05:54:47+00:00

I am using Codeigntier and I have the following dropdown in my view file

  • 0

I am using Codeigntier and I have the following dropdown in my view file which populates a list of subjects.

<?php echo form_dropdown('subject1', $dropdown_subjects,'',
 'class="required" id="subject1"'); ?>

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( SELECT teacherid from table3 WHERE subjectid=$subjectid) 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

Table Name: table3

subjectid teacherid
  1        1001
  2        1003

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).

Would you please kindly show me how my view and controller should look like if I want to achieve this?

Thanks 🙂

EDit

Hi, this is how my controller and view file looks like :

My Controller

   $id= $this->input->post('subject_id'); //receiving the ajax post from view 

   $this->db->select('teachername,teacherid');
   $this->db->from('subject_teacher');
   $this->db->join('teacher', 'teacher.teacherid = subject_teacher.teacherid');
   $this->db->where('subjectid',$id);
   $records = $this->db->get('');

   $data=array();
   $data[''] = 'Select'; 
   foreach ($records->result() as $row)
    {
        $data[$row->teacherid] = $row->teachername;
    }

    return ($data); // I need help here... How to send the data as json?

My view:

   <script>
           $(function(){

                 $("#subject").change(function(){
            $.ajax({
            url: "<?echo base_url();?>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   

     </script>




  <?php echo form_dropdown('subject1', $dropdown_subjects,'',
   'class="required" id="subject1"'); ?>



   <select name="teacher" id="teacher">
    <option value="">Select</option>
  </select>

Please make the necessary changes in my View and Controller for me.

Thanks in Advance 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:54:49+00:00Added an answer on June 3, 2026 at 5:54 am

    You can do this by using jquery ajax. First you post subject_id to ajax page, ajax page will return the list of teacher in combo box and then the result is populated in the first page.

    $("#subject").change(function(){
        $.ajax({
            url: "your-ajax-page-url",
            data: {subject_id: $(this).val()},
            type: "post",
            success: function(msg){
            $("#teacher").html();
        })
    })
    

    This is the edited controller

       $id= $this->input->post('subject_id'); //receiving the ajax post from view 
    
       $this->db->select('teachername,teacherid');
       $this->db->from('subject_teacher');
       $this->db->join('teacher', 'teacher.teacherid = subject_teacher.teacherid');
       $this->db->where('subjectid',$id);
       $records = $this->db->get('');
    
       $output = null; 
       foreach ($records->result() as $row)
        {
            $output .= "<option value='".$row->teacherid."'>".$row->teachername."</option>";
        }
    
        echo $output; // HTML example
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following the tutorial here: http://www.jotorres.com/2012/01/using-jquery-and-ajax-with-codeigniter/ For my view, I have: <script type=text/javascript src=<?php
I'm using CodeIgniter (because it's awesome) and I have something like: <?php echo anchor(/,
I have the following php code using the codeigniter framework $this->form_validation->set_rules('money', 'Money', 'integer|required|xss_clean'); It
I am using Codeigniter message library. In my controller I have the following code
I am using CodeIgniter and I have two different controllers - login.php and site.php
i am using codeigniter. i have class Orders which with some methods like start_order(),
I'm using the following htaccess script so that I can hide index.php from the
I'm trying to parse a moderately large XML file (6mb) in php using simpleXML.
I'm using CodeIgniter and I have a controller which processes uploads using AJAX. The
I have a modal window that loads on index page using the following command,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.