0

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.

1 Answer 1

2

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,
Sign up to request clarification or add additional context in comments.

11 Comments

Ok..Then how can I retrieve the data from controller
write the actual problem which you have face.
what is the return of success: function(data) { alert("hi"); console.log(data); },
emp name need to be display in success function
show the json object which is return in success function console.log(data) ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.