私は codeigniter を使用していますが、jQuery$.post
関数を使用してデータを処理する際に問題がありました。ajax_get_subject_creditsubjectid
関数などに値を送信し、同じデータベース テーブル内の別のフィールドを取得したい。結果は別のテキスト フィールドに表示されます。これが私のコードです。
意見:
$.post('<?php echo site_url('academic/ajax_get_subject_credit'); ?>', {'subjectid':subjectid}, function(data){
$('#chours' + id).val(data); });
これはドロップダウンから値を取得し、テキストフィールドにドロップダウンから自動入力したいと考えています。#chours
はテキスト フィールド ID です。
コントローラ:
function ajax_get_subject_credit($result)
{
$this->db->select('subjectid, subjectcredit');
$query = $this->db->get('ref_subject');
$result = $query->result_array();
$query->free_result();
$subjectid = array();
foreach($result as $row)
{
$result = $result + array($row['subjectid'] => $row['subjectcredit']);
}
return $result;
}
コントローラで変更
また、フィールドを直接呼び出すためにコントローラーでこのステートメントを使用しようとしましたが、まだ成功しませんでした:
function ajax_get_subject_credit($subjectid)
{
$this->db->select('subjectid, subjectcredit');
$this->db->where('subjectid',$subjectid);
$query = $this->db->get('ref_subject');
$credithour = $query->row()->subjectcredit;
$query->free_result();
echo $credithour;
}