Php AJAX和jQuery搜索代码点火器
我正在尝试对我的应用程序进行AJAX搜索,但我遇到了一些问题 我有带有字段id、标题、摘要和内容的表帖子。我想按标题搜索帖子 在我的控制器中,我加载模型帖子,我的索引函数如下所示Php AJAX和jQuery搜索代码点火器,php,jquery,mysql,ajax,codeigniter,Php,Jquery,Mysql,Ajax,Codeigniter,我正在尝试对我的应用程序进行AJAX搜索,但我遇到了一些问题 我有带有字段id、标题、摘要和内容的表帖子。我想按标题搜索帖子 在我的控制器中,我加载模型帖子,我的索引函数如下所示 function index() { $search= $this->input->post('search'); $query = $this->posts->search_posts($search); echo json_
function index()
{
$search= $this->input->post('search');
$query = $this->posts->search_posts($search);
echo json_encode ($query);
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/style.css');?>">
<script type="text/javascript" language="javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/jquery.js"></script>
<script type="text/javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/json2.js"></script>
<script>
$(document).ready(function(){
$("#search").keyup(function(){
if($("#search").val().length>3){
$.ajax({
type: "post",
url: "http://localhost/blogFinal/index.php/blog",
cache: false,
dataType: 'json';
data:'search='+$("#search").val(),
success: function(response){
$('#finalResult').html("");
var obj = JSON.parse(response);
if(obj.length>0){
try{
var items=[];
$.each(obj, function(i,val){
items.push($('<li/>').text(val.title + " " + val.summary));
});
$('#finalResult').append.apply($('#finalResult'), items);
}catch(e) {
alert('Exception while request..');
}
}else{
$('#finalResult').html($('<li/>').text("No Data Found"));
}
},
error: function(){
alert('Error while request..');
}
});
}
return false;
});
});
</script>
</head>
<body>
<div id="wrap">
<header>
<h1><?php echo anchor('','My Blog'); ?></h1>
</header>
<div id="container">
<div id="loginDiv"><?php echo anchor('admin/logout','Logout'); ?></div>
<hr/>
<input type="text" name="search" id="search" />
<ul id="finalResult"></ul>
</div>
<div style="clear: both;"> </div>
在我的model posts.php中,我放置了这个函数
function search_posts($search){
$this->db->select("id,title,summary,content");
$whereCondition = array('title' =>$search);
$this->db->where($whereCondition);
$this->db->from('posts');
$query = $this->db->get();
return $query->result();
}
我的观点是这样的
function index()
{
$search= $this->input->post('search');
$query = $this->posts->search_posts($search);
echo json_encode ($query);
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/style.css');?>">
<script type="text/javascript" language="javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/jquery.js"></script>
<script type="text/javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/json2.js"></script>
<script>
$(document).ready(function(){
$("#search").keyup(function(){
if($("#search").val().length>3){
$.ajax({
type: "post",
url: "http://localhost/blogFinal/index.php/blog",
cache: false,
dataType: 'json';
data:'search='+$("#search").val(),
success: function(response){
$('#finalResult').html("");
var obj = JSON.parse(response);
if(obj.length>0){
try{
var items=[];
$.each(obj, function(i,val){
items.push($('<li/>').text(val.title + " " + val.summary));
});
$('#finalResult').append.apply($('#finalResult'), items);
}catch(e) {
alert('Exception while request..');
}
}else{
$('#finalResult').html($('<li/>').text("No Data Found"));
}
},
error: function(){
alert('Error while request..');
}
});
}
return false;
});
});
</script>
</head>
<body>
<div id="wrap">
<header>
<h1><?php echo anchor('','My Blog'); ?></h1>
</header>
<div id="container">
<div id="loginDiv"><?php echo anchor('admin/logout','Logout'); ?></div>
<hr/>
<input type="text" name="search" id="search" />
<ul id="finalResult"></ul>
</div>
<div style="clear: both;"> </div>
你能在控制器中得到帖子['search']吗?你检查过了吗?我没有提到我得到了像这样的输出[{“id”:“1”,“title”:“我的第一篇帖子”,“summary”:“这是我的第一篇帖子!!!”,“content”:“这是我的第一篇帖子,你应该知道。”@asef seferov我不知道该检查什么?对不起,我是CodeIgniter的新手。你没有提到你遇到了什么问题。如果你在$('finalResult').html(“”)中设置了一个断点,它是否到达了那里?如果没有,通过记录json#u encode($query)的输出来检查PHP的响应是否确实是你认为的在将其发送回代码之前,请删除代码中的JSON.parse(response),当您使用数据类型:“JSON”时,ajax会进行解析;