2
我正在根據所選國家/地區的onchange()
事件提取城市數據。 我必須根據所選國家/地區填寫城市數據。Codeigniter:傳遞基於AJAX的數據
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>
這是我的下拉菜單:
<?php
if($value['type']=='countryname'){
if(isset($value['countryname'])){
echo '<div class="form-group">';
echo form_dropdown('countryname', $value['options'],'','class="form-control" id="changecountry"');
echo '</div>';
}
}
if($value['type']=='cityname'){
if(isset($value['cityname'])){
echo '<div class="form-group">';
echo form_dropdown('cityname',$value['options'],'','class="form-control"');
echo '</div>';
}
}
在成功的功能我得到的JSON數據,我不知道如何將此傳遞給下拉菜單:
["Dubai","Sharjah","Abu Dhabi","Ajman","Ras al-Khaimah","Umm al-Quwain"]