2016-05-27 12 views
0

私は2つのselectボックスを持っています。最初の値は以下の通りです。Codeigniter Ajaxは他の選択値に基づいて選択ボックスに値を挿入します

Select Boxes

MySQL次の表は、のようなものです。

MySQL Table

誰もが最初selectボックスからGeneral Streamを選択した場合、第二selectボックスのオプションは

<option value="1">Honours</option> 
<option value="2">General</option> 

...のようになります。しかし、誰が最初にselectボックス、二selectボックスからVocationalを選択した場合オプションは次のようになります...

<option value="2">General</option> 
controller

がこれを行っている ...

function get_coursetype() 
{ 
    $r=$this->input->post("coursetype"); 
    $_SESSION['coursetype']=$r; 
    //print json_encode($_SESSION['coursetype']); 
    if($r=='Honours'){ 
     $qry = $this->db->query('SELECT * FROM course'); 
     $data = array_shift($qry->result_array()); 
     echo json_encode($data['curs']); 
    } else { 
     $qry = $this->db->query('SELECT * FROM course WHERE curs="General"'); 
     $data = array_shift($qry->result_array()); 
     echo json_encode($data['curs']); 
    } 
} 

とAjaxで、私はこれを行っている

...

$(function(){ // start of doc ready. 
    $("#CourseType").change(function(e){ 
    e.preventDefault(); // stops the jump when an anchor clicked. 
    var coursetype = $(this).val(); // anchors do have text not values. 

    $.ajax({ 
    url: 'index.php?/admission/get_coursetype', 
    data: {'coursetype': coursetype}, // change this to send js object 
    type: "post", 
    success: function(data){ 

     loc = data; 
     $('#course').empty(); 
     $('#course').show(); 
     $.each(loc,function(data){ 
      $('#course').append($("<option></option>").attr("value",data).text(data)); 
     }); 
    } 
    }); 
    }); 
}); 
それがエラーを示している

... error_msg

どうすればいいですか?全体のシナリオが混乱しても厳しい

答えて

2

することは、私はあなたがarray_shiftで達成したいのか理解していない - ちょうどあなたの各ループでMySQLのキーを使用

function get_coursetype() 
{ 
    $r=$this->input->post("coursetype"); 
    $_SESSION['coursetype']=$r; 
    //print json_encode($_SESSION['coursetype']); 
    if($r=='1'){ 
     $qry = $this->db->query('SELECT * FROM course'); 
     $data = $qry->result_array(); 
    } else { 
     $qry = $this->db->query('SELECT * FROM course WHERE curs="General"'); 
     $data = $qry->result_array(); 
    } 
    echo json_encode($data); 
} 

とあなたのAJAXクエリで代わりにこれを試してください

$(function() 
{ // start of doc ready. 
    $("#CourseType").change(function(e) 
    { 
     e.preventDefault(); // stops the jump when an anchor clicked. 
     var coursetype = $(this).val(); // anchors do have text not values. 

     $.ajax(
     { 
      url: 'index.php?/admission/get_coursetype', 
      data: {'coursetype': coursetype}, // change this to send js object 
      type: "post", 
      success: function(data) 
      { 
       loc = data; 
       $('#course').empty(); 
       $('#course').show(); 
       $.each(loc,function(data){ 
        $('#course').append($("<option></option>").attr("value",data.id).text(data.curs)); 
       }); 
      } 
     }); 
    }); 
}); 
+1

お返事ありがとうございます。私は誤って 'array_shift'を使用しました。しかし、私は自分のコードを変更し、私は望みの結果を得ました。答えをありがとう。 – Raj

+0

これは私の問題を説明するために作成しました。申し訳ありません。 – Raj

+0

hehe np、私が "混乱"を意味するものは、あなたのモデルにあなたのデータハンドリングを残しておいてください。コントローラのクエリはモデルの目的を脇に置いていて、クライアント側はバックボーンやハンドルバーなどについて考えるかもしれません。 ) – sintakonte

関連する問題