2016-03-15 22 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); 
}