Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript codeigniter中的依赖下拉列表_Javascript_Php_Codeigniter - Fatal编程技术网

Javascript codeigniter中的依赖下拉列表

Javascript codeigniter中的依赖下拉列表,javascript,php,codeigniter,Javascript,Php,Codeigniter,当选择国家时,我需要在下拉列表中填写国家列表 我的代码 看法 模型 模型 function fetch_states($postData) { //$query = $this->db->query("SELECT * FROM states"); //return $query; $response = array(); // Select record $this->d

当选择国家时,我需要在下拉列表中填写国家列表

我的代码

看法

模型

模型

function fetch_states($postData)
    {
             //$query = $this->db->query("SELECT * FROM states");
            //return $query;
        $response = array();
        // Select record
        $this->db->select('id,name');
        $this->db->where('country_id', $postData['country']);
        $q = $this->db->get('states');
        $response = $q->result_array();
        $response= json_encode($response);
        return $response;
    }
模型从数据库返回值,也可以从视图访问该值。 我提醒响应,它返回类似{“id”:“36”,“name”:“drish”}的数组。 我需要在这个结果下拉列表中填入id状态


有什么帮助吗?提前感谢

试试这个
型号:

function fetch_states($country)
{
    $this->db->select('id,name');
    $this->db->where('country_id', $country);
    $q = $this->db->get('states');
    if ($q->num_rows() > 0) {
        return $q->result_array();
    }else{
        return array();
    }
}
function get_states()
{
    $this->load->helper('url');
    $postData = $this->input->post();
    $this->load->model('candidate_model');
    $country = $postData['country'];
    $state = $this->candidate_model->fetch_states($country);
    $respose_array['state'] = $state;
    echo json_encode($response_array);
    exit();
}
$.ajax({
    url:'<?php echo base_url()?>main/get_states',
    method: 'post',
    data: {country: country},
    dataType: 'json',
    success: function(response){
      $.each(JSON.parse(response.state),function(i,item){
         $('#state').append('<option value="'+item.id+'">'+item.name+'</option>');
      });
    }
});
控制器:

function fetch_states($country)
{
    $this->db->select('id,name');
    $this->db->where('country_id', $country);
    $q = $this->db->get('states');
    if ($q->num_rows() > 0) {
        return $q->result_array();
    }else{
        return array();
    }
}
function get_states()
{
    $this->load->helper('url');
    $postData = $this->input->post();
    $this->load->model('candidate_model');
    $country = $postData['country'];
    $state = $this->candidate_model->fetch_states($country);
    $respose_array['state'] = $state;
    echo json_encode($response_array);
    exit();
}
$.ajax({
    url:'<?php echo base_url()?>main/get_states',
    method: 'post',
    data: {country: country},
    dataType: 'json',
    success: function(response){
      $.each(JSON.parse(response.state),function(i,item){
         $('#state').append('<option value="'+item.id+'">'+item.name+'</option>');
      });
    }
});
查看ajax代码:

function fetch_states($country)
{
    $this->db->select('id,name');
    $this->db->where('country_id', $country);
    $q = $this->db->get('states');
    if ($q->num_rows() > 0) {
        return $q->result_array();
    }else{
        return array();
    }
}
function get_states()
{
    $this->load->helper('url');
    $postData = $this->input->post();
    $this->load->model('candidate_model');
    $country = $postData['country'];
    $state = $this->candidate_model->fetch_states($country);
    $respose_array['state'] = $state;
    echo json_encode($response_array);
    exit();
}
$.ajax({
    url:'<?php echo base_url()?>main/get_states',
    method: 'post',
    data: {country: country},
    dataType: 'json',
    success: function(response){
      $.each(JSON.parse(response.state),function(i,item){
         $('#state').append('<option value="'+item.id+'">'+item.name+'</option>');
      });
    }
});
$.ajax({
url:'main/get_states',
方法:“post”,
数据:{国家:国家},
数据类型:“json”,
成功:功能(响应){
$.each(JSON.parse(response.state),函数(i,item){
$('#state').append(''+item.name+'');
});
}
});

不要在模型中进行json编码,只需按原样发送,在需要的控制器中执行一次即可did@M.Hemant,好的,让我检查一下现在的反应是,[对象对象]我知道了,谢谢