在Codeigniter中使用Jquery根据另一个下拉列表的选择填充下拉列表 [英] Populating a dropdown based on the selection of another dropdown using Jquery in Codeigniter
问题描述
我正在使用Codeigntier,并且在查看文件中有以下下拉列表,其中包含主题列表.
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"'); ?>
现在,当任何人从上面的下拉列表中选择任何值时,我想使用jquery将值发送到我的控制器并在下表中查询( SELECT teacherid from table3 WHERE subjectid=$subjectid)
以获取Teacherid,以便我可以在另一个下拉列表中填充Teacherid列表选择.如果有任何用户从第一个下拉菜单更改了选择,我也想更改第二个下拉菜单的值
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
表名称:table3
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?
谢谢:)
这就是我的控制器和视图文件的样子:
Hi, this is how my controller and view file looks like :
我的控制器
$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?
我的观点:
<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.
在此先感谢:)
推荐答案
您可以使用jquery ajax来做到这一点.首先,您将 subject_id 发布到ajax页面,ajax页面将在组合框中返回教师列表,然后在第一页中填充结果.
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();
})
})
这是已编辑的控制器
$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
这篇关于在Codeigniter中使用Jquery根据另一个下拉列表的选择填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!