Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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
Php 使用ajax、codeigniter、下拉菜单进行更改_Php_Ajax_Codeigniter_Onchange - Fatal编程技术网

Php 使用ajax、codeigniter、下拉菜单进行更改

Php 使用ajax、codeigniter、下拉菜单进行更改,php,ajax,codeigniter,onchange,Php,Ajax,Codeigniter,Onchange,我在codeigniter中遇到了onchange事件的问题…我正在显示一个国家/地区状态onchange事件,但该国家/地区的值为零,因此它不会相应地更改状态 控制器: <?php class countries extends CI_Controller { public function __construct() { parent::__construct(); $this->load->database()

我在codeigniter中遇到了onchange事件的问题…我正在显示一个国家/地区状态onchange事件,但该国家/地区的值为零,因此它不会相应地更改状态

控制器:

    <?php
    class countries extends CI_Controller {
   public function __construct()
   { 
            parent::__construct();
    $this->load->database();
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->model('countries_model');

    $this -> load -> model('country_model');
    $this -> load -> model('state_model');

}

public function index()
{
$this->data['countries'] = $this -> country_model -> get_countries();
  $view_data = array();
  $this->display_data();
$this->load->view('view', $this->data);
}


    public function get_states(){
        log_message('debug', 'Inside uController :get_states');
        log_message('debug', 'Value of country is '+$this->input->post('country_id'));
        log_message('debug', 'Value of country is '+$this->input->post('data'));
        var_dump(data);
        $country=$this->input->post('country');
        //echo $country;

   $this->load->model('state_model');
  header('Content-Type: application/x-json; charset=utf-8');
  echo(json_encode($this->state_model->get_states($country)));
    }
}


public function dataupdate()
{
      $i=$this->input->post('country_id');
    $s=$this->input->post('state_id');
        $this->model->UpdateData();
        $this->load->view('success');
    }

?>

首先,在您的控制器中没有您在ajax中调用的函数名
get_states
。只有
get_cities
功能。您必须更改ajax调用以获取城市或重命名函数

然后,如果未使用$(“#country').val()获取所选选项,请重试

$('#country option:selected').val()  

接下来,您必须在ajax函数中指定
数据类型
to json,以将响应视为json

控制器功能

 public function get_cities(){
        log_message('debug', 'Value of country is '+$this->input->post('country_id'));
        $country=$this->input->post('country');
        $this->load->model('state_model');
        $states = array();
        $states = $this->state_model->get_states($country);
        echo json_encode($states);
  }
查看

$('#country').change(function(){
    $.ajax({
        type: "POST",
        url: "<?php echo site_url('countries/get_cities'); ?>", 
        data:country_id,
        dataType:"json",//return type expected as json
        success: function(states){
               $.each(states,function(key,val){
                    var opt = $('<option />'); 
                    opt.val(key);
                    opt.text(val);
                    $('#states').append(opt);
               });
        },
    });
});
$('#country')。更改(函数(){
$.ajax({
类型:“POST”,
url:“”,
数据:国家/地区id,
数据类型:“json”,//返回类型应为json
成功:功能(状态){
$。每个(状态、功能(键、值){
var opt=$('');
选择值(键);
选择文本(val);
$('#states')。附加(opt);
});
},
});
});

什么是
u/
url:“
?@Renier:将其更改为适当的控制器名称(u也是我的控制器之一)@Nauphal.M:没有帮助,我遇到了相同的问题…值没有传递到控制器,我得到的值为0,这是错误的控制器函数名称是错误的。检查更新后的answeerHey@Nouphal…我实际上使用了国家、州、城市函数并应用了onchange,所以有点困惑,但是的,我在原始查询中输入了正确的名称,但为了粘贴一个短代码,我只发布了国家和国家的两个函数…我将进行必要的更改。。。我仍然没有得到该值,它在我的警报中显示为[object object]。。我的日志显示0为valuelog_消息('debug','country的值为'+$this->input->post('country');该值在日志文件中给我0…应该给我所选的国家id…@Nouphal.M
public function get_countries() {

            $this -> db -> select('pkCountry, CountryName');
            $query = $this -> db -> get('MCountry');

            $countries = array();

            if ($query -> result()) {
                    foreach ($query->result() as $country) {
                            $countries[$country -> pkCountry] = $country -> CountryName;
                    }
                    return $countries;
            } else {
                    return 0;
            }
    }
$('#country option:selected').val()  
   $(this).find(':selected').val()
 public function get_cities(){
        log_message('debug', 'Value of country is '+$this->input->post('country_id'));
        $country=$this->input->post('country');
        $this->load->model('state_model');
        $states = array();
        $states = $this->state_model->get_states($country);
        echo json_encode($states);
  }
$('#country').change(function(){
    $.ajax({
        type: "POST",
        url: "<?php echo site_url('countries/get_cities'); ?>", 
        data:country_id,
        dataType:"json",//return type expected as json
        success: function(states){
               $.each(states,function(key,val){
                    var opt = $('<option />'); 
                    opt.val(key);
                    opt.text(val);
                    $('#states').append(opt);
               });
        },
    });
});