简体   繁体   English

如何使用Codeigniter从基于另一个选择标签的值填充选择标签

[英]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.

相关问题 如何根据来自数据库的值更改选择标签值 - how to change select tag values based on value coming from database 使用第二选择标签的值填充第三选择标签,第二选择标签的值使用第一选择标签从数据库中获取 - Populate the third select tag using the value of second select tag which has its values from the database using the first select tag 使用第二选择标签的值填充第三选择标签,第二选择标签的值使用第一选择标签从数据库中获取 - Populate the third select tag using the value of second select tag which has its values from the database using the first select tag 在php中填充选择标签 - populate select tag in php 如何从选择标记中获取两个值 - How to get two values from select tag 从另一个选择中更改选择标记中的选项 - Change option in select tag from another select 如何使用PHP根据第一个选择标签中选择的选项填充第二个选择标签? - How do I use PHP to populate a second select-tag with options based on the option selected in the first select-tag? 基于另一个 Select 填充 Select - Populate Select based on another Select 当我选择另一个选择标签时如何更改选择标签? - How to change select tag when I choose another select tag? 如何在php中的另一个选择标签中调用选择标签ID - How to call select tag id in another select tag in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM