简体   繁体   中英

How to populate a select tag from values based on another select tag with codeigniter

Please forgive me if I make any mistakes in english, its not really my best language. I will edit this post if I make errors please let me know.

I'm trying to make a php page with Codeigniter with a select tag whose values are from a database, and am trying to populate a second select tag based from the value placed on the former.

My college_subj database have three columns. CollCode, SC, and Subj.

Basically, the College Code(CollCode) and Subject Code(SC) have combinations. A College Code X can have A,B,C SC, and College Code Y can have B,C,D,E SC. I am trying to make those SC appear on my second select tag when the first select tag, the CollCode, has a value.

The page's function is accept values from the two select tags and insert it into the database.

Here's the select tags on my edit.php:

 <form method="post" action="<?php echo base_url();>index.php/Controller/insertfunction" id="crq"> <h3>Select College Code:</h3> <select id="codecrq" name="code"> <option value="" selected="selected">---Select College Code---</option> <?php foreach ($code as $row4): ?> <option label="<?php echo $row4['Code']; ?>" value="<?php echo $row4['Code']; ?>" <?php echo set_select('code', $row4['Code'], False); ?>> <?php echo $row4['Code'] ; ?> </option> <?php endforeach; ?> </select> <h3>Select SC:</h3> <select id="sccrq" name="sc"> <option value="" selected="selected">---Select SC---</option> </select> </form> 

Here's how I get the values that I placed in the Code select tag which is in the Model:

public function Code() {  
    $this->db->distinct();
    $this->db->select('college_subj.CollCode');
    $this->db->from('college_subj');

    $query = $this->db->get();
    return $query->result_array();
}

Here's a jquery that I try to use to populate the SC select tag:

 $("#codecrq").change(function(){ var selectedMark = $("code").val(); if(selectedMark !== ""){ $.ajax({ type: "GET", url: "Controller/sccrq/" + selectedMark, success: function(data){ $("#sccrq").html(""); $("#sccrq").append("<option value=''></option>"); $.each(data, function(){ $("#sccrq").append("<option value='" + this.sc + "'>" + this.sc + "</option>"); }); } }); } }); 

and here's the sccrq code from my controller, which is supposed to get the SCs based from the College Code combination and pass it on the SC select tag:

function sccrq($code){
    $this->db->distinct();
    $this->db->select('college_subj.sc');
    $this->db->from('college_subj');
    $this->db->where($code);
    $query = $this->db->get()->result_array();

    return $query;
}

I try making it run, but nothing comes out from the SC select tag.

Any help will be deeply appreciated! Thank you for you're time!

You can try

function sccrq($code){
    $query = $this->db->query("SELECT SC FROM college_subj WHERE code = '".$code."'");
    return $query;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM