View:
<select class="form-control" id="emp_id" name="emp_id">
<option value="">--- Select Emp id ---</option>
<?php foreach($employee as $emp){?>
<option value="<?php echo $emp->id;?>"><?php echo $emp->id;?></option>
<?php }?>
</select>
<div id="home"></div>
<script>
$('select[name="emp_id"]').on('change', function() {
var id=$("#emp_id").val();
$.ajax({
url: "<?php echo base_url(); ?>/employee_master"+id,
dataType: 'json',
type: 'post',
success: function(data) {
alert("hi");
console.log(data.res);
},
error: function( error )
{
alert( error );
}
});
return false;
});
</script>
Controller:
public function employee_master($id)
{
$data['res']=$this->payslip_model->fetch_employee_name($id);
echo json_encode($data);
}
Model:
public function fetch_employee_name($id)
{
$this->db->select('name');
$this->db->from('employee_master');
$this->db->where('id',$id);
$res=$this->db->get();
return $res->result();
}
In above code,I am using dataType:'json' in ajax, it will always execute error function in ajax.If I am not using dataType:'json' it works fine, but I cannot know how to retrieve the data from controller to ajax.Please help me to find out the error.