无法从视图将参数传递到控制器


Unable to pass parameter to controller from view

我要实现的是,当我从下拉列表中选择一个人时,他的电话号码应该出现在下拉列表的"更改时"功能的相邻文本框中。我已经能够从下拉列表中的数据库中获取人名。

这是模型代码:

 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"传递给控制器函数。

您好,您在 get 方法中传递数据并声明 ajax post 方法,以便更改数据类型或传递数据

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);
}