Reputation: 144
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.
Upvotes: 0
Views: 2696
Reputation: 81
yes it will give you error when you use data type json. it means that on ajax call the data will be pass to controller in json format.. while you are passing a simple post data...
remove the datatype json. every thing is ok...
and more mistake use this line
url: "<?php echo base_url(); ?>/contorller_name/employee_master/"+id,
Upvotes: 2