这里需要一些帮助,我一直在这里搜索相关问题,但似乎没有答案。好的,进展如何
我有一个简单的搜索功能,可以搜索我的数据库,我使用ajax传递数据并返回响应,我设法做到了这一点,但我的问题是,我似乎无法按照我想要的方式显示响应。
这是我的Ajax
$.ajax({
url: url, /// defined url
type: type, ///defined type
data: data, ///defined data
success: function(response){
//here I want to display something like
$('#display').html(the name of the employee);
}
});
这是ajax响应
{
"employee": [{
"badgeno": "123 ",
"name": "John G. Doe",
"success": true
}]
}
{
"employee": [{
"badgeno": "456 ",
"name": "Jane G. Doe",
"success": true
}
我想在那里获得员工姓名,并将其显示在我的页面中。我到底该怎么做?
提前谢谢。我还是BTW 的新手
这是PHP
$getEmp = $this->Employee_model->search_emp($employee);
$count = count($getEmp);
if($getEmp){
for ($i=0; $i < $count; $i++) {
$data['employee'][$i] = array(
'badgeno' => $getEmp[$i]->BADGENO,
'name' => $getEmp[$i]->NAME,
'success' => true
);
echo json_encode($data);
}
print_r($data);
//$this->load->view('admin/home', $data);
}
尝试:
employee_name= data.employee[0].name;
$('#display').html(employee_name);
链接到fiffle:https://jsfiddle.net/fcz53htw/如果您有多个名称,请先将它们添加到数组中,然后再打印数组的json_encode。现在它不起作用了,因为你打印了两次。尝试将您的php更改为:
$getEmp = $this->Employee_model->search_emp($employee);
$count = count($getEmp);
if($getEmp){
for ($i=0; $i < $count; $i++) {
$data['employee'][$i] = array(
'badgeno' => $getEmp[$i]->BADGENO,
'name' => $getEmp[$i]->NAME,
'success' => true
);
//echo json_encode($data);
}
echo json_encode($data);
//$this->load->view('admin/home', $data);
}
在php上,您要写两次结果,删除打印机仅使用json_encode