[英]ajax jquery does not return select query results
So, I have a textfield n my view file that gets the employee code and jquery calls a function in the controller for employees details to fill in some fields in the same view page immediately. 因此,我的视图文件中有一个文本字段,该文本字段获取员工代码,并且jquery调用控制器中的一个函数,以使员工详细信息立即填充同一视图页面中的某些字段。 But I am stock and nothing is really happening, see in my codes and help me do it.
但是我有货,实际上什么也没发生,请查看我的代码并帮助我做到。 Thank you!
谢谢!
jquery: jQuery的:
$('#employee_code').change(function(){
var em_code = $('#employee_code').val();
if (em_code != ""){
var post_url = "<?php echo base_url();?>index.php/it_inventory/get_employee_details/" + em_code;
$.ajax({
type: "POST",
url: post_url,
success: function(name,position,department)
{
$('#employee_name').val(name);
$('#employee_position').val(position);
$('#employee_department').val(department);
}
});
}
});
CI controller: CI控制器:
function get_employee_details($em_code="") {
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->it_inventory_model->get_employee_details($em_code)));
}
CI Model CI模型
function get_employee_details($em_code){
$q = "select name,position,department from employees where code=?";
$query_em_detail=$this->db->query($q,$em_code);
$outputs = array();
if($query_em_detail->result()){
foreach ($query_em_detail->result() as $output) {
$outputs['name'] = $output->name;
$outputs['position'] = $output->position;
$outputs['department'] = $output->department_code;
}
return $outputs;
} else {
return FALSE;
}
}
you should add dataType
in your ajax request, like: 您应该在ajax请求中添加
dataType
,例如:
$.ajax({
type: "POST",
url: post_url,
dataType: "json",
success: function(data) {
console.log(data); //response from your controller
console.log( data.name); //to get name
},
error: function() { alert("Something went wrong!"); }
});
and controller: 和控制器:
function get_employee_details($em_code="") {
header('Content-Type: application/json',true);
echo(json_encode($this->it_inventory_model->get_employee_details($em_code)));
}
I think success: function(name,position,department)
should be success: function(result)
. 我认为
success: function(name,position,department)
应该success: function(result)
。 Then you need to use result.name
, result.position
etc 然后,您需要使用
result.name
, result.position
等
success: function(result) {
$('#employee_name').val(result.name);
$('#employee_position').val(result.position);
$('#employee_department').val(result.department);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.