Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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:传递基于ajax的数据_Javascript_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Javascript Codeigniter:传递基于ajax的数据

Javascript Codeigniter:传递基于ajax的数据,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我正在根据所选国家的onchange()事件获取城市数据。 我必须根据所选国家填充城市数据 class ajax extends CI_Controller { public function getcities(){ if($this->input->post('countryId')){ $countryId= $this->input->post('countryId'); } fore

我正在根据所选国家的
onchange()
事件获取城市数据。 我必须根据所选国家填充城市数据

class ajax extends CI_Controller {
    public function getcities(){
        if($this->input->post('countryId')){
            $countryId= $this->input->post('countryId');
        }
        foreach ($this->user_model->getselectedcity($this->input->post('countryId')) as $key => $value) {
             $cities[] = $value->city; 
        }
        echo json_encode($cities);
    }
 }
在我看来,我有以下脚本:

<script type="text/javascript">
    $(document).ready(function (){
        $('#changecountry').change(function(){
            //alert($(this).val());
            $.ajax({
                method: "POST",
                url: "<?php echo base_url()?>index.php/ajax/getcities",
                data:{countryId:$(this).val()}, 
                success: function(data){
                    console.log(data);
                }
            });
        });
    });
</script>

您需要使用ID属性在城市下拉列表中附加Ajax响应,如:

<script type="text/javascript">
    $(document).ready(function (){
        $('#changecountry').change(function(){
            $.ajax({
                method: "POST",
                url: "<?php echo base_url()?>index.php/ajax/getcities",
                data:{countryId:$(this).val()}, 
                success: function(response){
                    $.each(response, function (index, value) {
                        $("#selectID").append("<option value='"+value+"'>" + value + "</option>");
                    });
                }
            });
        });
    });
</script>

$(文档).ready(函数(){
$('#changecountry').change(function(){
$.ajax({
方法:“张贴”,
url:“index.php/ajax/getcities”,
数据:{countryId:$(this.val()},
成功:功能(响应){
$.each(响应、函数(索引、值){
$(“#selectID”).append(“+value+”);
});
}
});
});
});
您的下拉列表(在此处添加ID属性):

<?php
if($value['type']=='cityname'){
    if(isset($value['cityname'])){
        echo '<div class="form-group">';
        echo form_dropdown('cityname',$value['options'],'','class="form-control" id="selectID"');
        echo '</div>';
    }
} 
?>

在控制器中

foreach ($this->user_model->getselectedcity($this->input->post('countryId')) as $option) {
     $cities[] = '<option value="'.$option["city"].'">'.$option["city"].'</option>';
}
HTML格式的

<select class="form-control" id="state" name="state">
    <option selected="selected">--Select State--</option>
</select>

--选择状态--
foreach ($this->user_model->getselectedcity($this->input->post('countryId')) as $option) {
     $cities[] = '<option value="'.$option["city"].'">'.$option["city"].'</option>';
}
success: function(html)
{
    $("#state").html(html);
}
<select class="form-control" id="state" name="state">
    <option selected="selected">--Select State--</option>
</select>