Codeigntierを使用していますが、ビューファイルに次のドロップダウンがあり、サブジェクトのリストが表示されます。
<?php echo form_dropdown('subject1', $dropdown_subjects,'',
'class="required" id="subject1"'); ?>
誰かが上のドロップダウンから値を選択したら、次の表のjqueryとqueryを使用して値をコントローラーに送信し、teacherid( SELECT teacherid from table3 WHERE subjectid=$subjectid)
を取得して、別のドロップダウン選択でteacheridリストに入力できるようにします。ユーザーが最初のドロップダウンから選択を変更した場合、2番目のドロップダウンの値も変更したい
テーブル名:table3
subjectid teacherid
1 1001
2 1003
つまり、重要なのは、別のドロップダウンに基づいてドロップダウンにデータを入力したいということです。このトピックに関するチュートリアルをいくつか見つけましたが、それらを本当に理解することはできませんでした(私は愚かだと知っています)。
これを実現したい場合、ビューとコントローラーがどのように表示されるかを教えてください。
ありがとう :)
編集
こんにちは、これは私のコントローラーとビューファイルがどのように見えるかです:
私のコントローラー
$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>
ビューとコントローラーに必要な変更を加えてください。
前もって感謝します :)