2

私は 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;
}
4

4 に答える 4

7

ここでは一般的な例を示します

ビューファイルで

$.post('<?php echo site_url("test/test"); ?>', {'id':1}, function(response){

   if(response.success)
   {
     alert(response.message);
   } else
   {
     alert('Something went wrong!!');
   }
}, 'json');

コントローラー Test.php で

function test()
{
  $id = $this->input->post('id');

  //do additional stuff

  $result = 'i am coming right out of controller!! ';

  echo json_encode(array('success' => true, 'message' => $result));
}
于 2013-04-01T16:41:52.920 に答える
3

AJAX に値を返すためにreturnを使用しないでください。使用するecho

これを変える、

return $result;

echo $result;
于 2013-04-01T15:11:29.793 に答える
0

javascript がドロップダウンにデータを入力するために使用できる配列をメソッドが返すようにしたい場合は、おそらく文字列を返したくないでしょう。

次のようなことを試してください:

function ajax_get_subject_credit()
{
    $query = $this->db->select('subjectid, subjectcredit')->get('ref_subject');

    $result = $query->result();

    $out = array();
    foreach($result as $row) {
        $out[$row->subjectid] = $row->subject_credit;
    }

    header('Content-Type: application/json');
    echo json_encode($out);

}

これにより、JSON 配列がビューに返されます。これを使用して、JavaScript メソッドでドロップダウンに値とラベルを設定できます。

于 2013-04-01T15:31:08.963 に答える
0

ここに私の結果があります:

ビューで :

function subjectid_change(id, subjectid){
    //Set value
    setValue(id, subjectid);

    $.post('<?php echo site_url('academic/ajax_get_subject_credit'); ?>', {'subjectid':subjectid}, function(response){

        if(response.success)
        {
          $('#chours' + id).val(response.value);
        } else
        {
          alert('Something went wrong!!');
        }
    }, 'json'); }

そして私のコントローラー:

function ajax_get_subject_credit()
{
    //Get Post Value
    $subjectid = $this->input->post('subjectid');
    //Select subjectid,subjectcredit FROM
    $this->db->select('subjectid, subjectcredit');
    //Where subjectid = 'subjectid'
    $this->db->where('subjectid',$subjectid);
    //Database name
    $query = $this->db->get('ref_subject');

    $credithour = $query->row()->subjectcredit;
    $query->free_result();
    $result = $credithour;

    echo json_encode(array('success' => true, 'value' => $result));
}

私を助けてくれたみんなに感謝します。

于 2013-04-02T07:41:51.943 に答える