I am fetching cities data based on the onchange() event against selected country.
I have to populate cities data based on country selected.
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);
}
}
In my view I have this script:
<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>
These are my dropdowns:
<?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>';
}
}
In the success function I'm getting json data, I'm not sure how to pass this to the dropdown:
["Dubai","Sharjah","Abu Dhabi","Ajman","Ras al-Khaimah","Umm al-Quwain"]