带有CI的php上的Ajax,获取未定义的索引项
我是PHP新手,我学习了一个教程,在live search engine中键入字符时没有得到任何结果。我怀疑我在哪里查看指向源的索引,但我不确定。抱歉,如果代码太长 Controller product.php带有CI的php上的Ajax,获取未定义的索引项,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我是PHP新手,我学习了一个教程,在live search engine中键入字符时没有得到任何结果。我怀疑我在哪里查看指向源的索引,但我不确定。抱歉,如果代码太长 Controller product.php <?php class Product extends CI_Controller { public function index(){ $this->load->view('index'); $this->lo
<?php
class Product extends CI_Controller {
public function index(){
$this->load->view('index');
$this->load->model('mproduct');
$result = $this->mproduct->search($_GET['term']);
echo "this is search";
if(count($result)>0) {
foreach ($result as $pr)
$arr_result[] = $pr->name;
}
echo json_encode($arr_result);
}
}
<?php
class MProduct extends CI_Model
{
function __construct() {
parent::__construct();
}
function search($name) {
$this->db->like('name',$name, 'both');
return $this->db->get('product')->result();
}
}
?>
view index.php
<!DOCTYPE html>
<html lang ="en">
<head>
<link href="/phpAmir_contract/assets/css/jquery-ui.theme.min.css" type="text/css" rel="stylesheet">
<script type="text/css" src="/phpAmir_contract/assets/css/jquery-ui.min.css"></script>
<script type="text/css" src="/phpAmir_contract/assets/css/jquery-ui.theme.min.css"></script>
<script type="text/javascript" src="/phpAmir_contract/assets/js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="/phpAmir_contract/assets/js/jquery-ui.min.js"></script>
<meta charset="utf-8">
<title> demo</title>
<script type="text/javascript">
$(document).ready(function()){
$('#production').autocomplete({
source: "/phpAmir_contract/application/controllers/product/search"
})
});
</script>
</head>
<body>
<input type="text" name ="productname" id="productname"/>
</body>
</html>
演示
$(文档).ready(函数()){
$(“#生产”)。自动完成({
来源:“/phpAmir_合同/应用程序/控制器/产品/搜索”
})
});
您的产品名称是通过GET var发送的还是通过url发送的?如果它是通过GET var发送的,为什么需要url末尾的“/?”?只能使用json进行一次回显,将
echo
移出循环…如果没有结果,还需要返回一些内容。是否转储了db查询数据以查看它是否返回您期望的结果?可以使用echo$db->last_query()
查看实际的sql语句,然后在phpMyAdminProblem 1中测试该语句:HTML中没有ID为production
的输入。问题2:打开控制台,转到“网络”选项卡,查看响应