[英]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. 我试图用Codeigniter制作一个带有选择标签的php页面,其值来自数据库,并试图根据放置在前者上的值填充第二个选择标签。
My college_subj database have three columns. 我的college_subj数据库有三列。 CollCode, SC, and Subj.
CollCode,SC和Subj。
Basically, the College Code(CollCode) and Subject Code(SC) have combinations. 基本上,大学代码(CollCode)和学科代码(SC)具有组合。 A College Code X can have A,B,C SC, and College Code Y can have B,C,D,E SC.
大学代码X可以具有A,B,C SC,而大学代码Y可以具有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.
当第一个选择标签CollCode具有值时,我试图使这些SC出现在我的第二个选择标签上。
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: 这是我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: 这是我获取放置在Model的Code select标记中的值的方法:
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: 这是我尝试用来填充SC select标签的jquery:
$("#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: 这是我的控制器的sccrq代码,应该从大学代码组合中获取SC,并将其传递给SC select标签:
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. 我尝试使其运行,但是SC select标签没有任何结果。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.