2016-03-15 21 views
0

將參數傳遞給控制器​​我試圖實現的是,當我從下拉列表中選擇一個人時,他的電話號碼應該出現在相鄰文本框中的下拉變化功能上。 我已經能夠在下拉菜單中從數據庫中獲取人名。無法從視圖

這裏的型號代碼:

public function get_phone_id($id) 
{ 
    $this->db->select('dealerphone'); 
    $this->db->where('dealerid',$id); 
    $query = $this->db->get('Dealer'); 
$result=$query->result_array(); 
    return $result; 


} 

控制器代碼:

public function dealer_phone($id) 
{ 

$list = $this->person->get_phone_id($id); 
    // print_r($list); 
    echo json_encode($list); 


} 

查看代碼:

<script> 


        $("#select").change(function() { 
         var getValue = $(this).find('option:selected').attr('id'); 
         get_phone(getValue); 
         }); 




        function get_phone(id) { 
         alert(id); 
         $.ajax({ 
          url: "<?php echo site_url('dealer_controller/dealer_phone')?>/" + id 
          , type: "POST" 
          , dataType: "JSON" 
          , async:false 
          , success: function() { 
           //if success reload ajax table 
           console.log(); 
          } 
          , error: function (xhr,status) { 
           console.log(status); 
          } 
         }); 


        } 




       </script> 

我現在面臨的唯一問題是,我無法通過「 getValue'到控制器功能。

+0

聲明AJAX POST方法你能檢查瀏覽器控制檯什麼是請求和響應值? – Tpojka

+0

請添加您的html – Vickel

回答

0

你好,你在GET方法傳遞數據等你改變數據類型或者傳遞數據

function get_phone(id) { 
         alert(id); 
         $.ajax({ 
          url: "<?php echo site_url('dealer_controller/dealer_phone')?>" 
          ,data :{id:your variable (id)} 
          , type: "POST" 
          , dataType: "JSON" 
          , async:false 
          , success: function() { 
           //if success reload ajax table 
           console.log(); 
          } 
          , error: function (xhr,status) { 
           console.log(status); 
          } 
         }); 
         } 

,你可以得到這個數據在控制器端這樣

public function dealer_phone() 
{ 
$list = $this->input->post('id'); 
    // print_r($list); 
    echo json_encode($list); 
}