controller
public function ajaxData(){
$country_id=$this->input->post('country_id',true);
$this->load->model('country_m');
$data['states']=$this->country_m->getStates($country_id);
echo json_encode($data);
$this->load->view('onlineappointment.tpl',$data,TRUE);
}
ajax
$(document).ready(function(){
$('#country').on('change',function(){
var countryID = $(this).val();
// alert(countryID);
if(countryID){
$.ajax({
type:'POST',
url:'http://localhost/new_jivaamri/index.php/OnlineAppointment123/ajaxData',
data:'country_id='+countryID,
success:function(html){
console.log(html);
console.log(typeof html);
var obj2 = JSON.parse(html);
$('#state').html(html);
$('#city').html('<option value="">Select state first</option>');
}
});
}else{
$('#state').html('<option value="">Select country first</option>');
$('#city').html('<option value="">Select state first</option>');
$('#address').html('<option value="">Select state first</option>');
}
});
I am receiving the ajax response output as shown below:
{"states":[{"state_id":"45","state_name":" Kabul","country_id":"1","active":"1"},{"state_id":"46","state_name":"Kandahar","country_id":"1","active":"1"},{"state_id":"47","state_name":"Herat","country_id":"1","active":"1"},{"state_id":"48","state_name":"Mazar-i-Sharif","country_id":"1","active":"1"},{"state_id":"49","state_name":"Kunduz","country_id":"1","active":"1"},{"state_id":"50","state_name":"Taloqan","country_id":"1","active":"1"},{"state_id":"51","state_name":"Jalalabad","country_id":"1","active":"1"},{"state_id":"52","state_name":"Puli Khumri","country_id":"1","active":"1"}]}
However, I am unsure how to extract the state names from the response.