//html+ajax
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文学查询</title>
</head>
<body>
<h2>文学搜索</h2>
<div>
<ul>
<li><input type="text" value="" id="keyword"><input type="button" value="SEARCH" onclick="s(1);"></li>
</ul>
<div id="result" style="height:200px;"></div>
<div id="pagnation"></div>
</div>
<script src="jquery.js"></script>
<script>
function s(pn)
{
var kd=$("#keyword").val();
$("#result").html("正在搜索中..");
$.ajax({
‘url‘:‘http://127.0.0.1/rikao/sousuo/1.php?kd=‘+kd+‘&pn=‘+pn+‘&pz=5&callback=showresult‘,
‘type‘:‘get‘,
‘dataType‘:‘jsonp‘,
‘jsonpCallback‘:‘showresult‘,
‘success‘:function(txt)
{
if (txt.success==1)
{
var lis=‘‘;
$.each(txt.data,function(i,v)
{
lis+="<li>"+v.name_1+"</li>"
});
$("#result").html("<ul>"+lis+"</ul>");
var pt=Math.ceil(txt.total/txt.pz);
var lia=‘‘;
for(var i=1; i<=pt;i++)
{
if (i==txt.pn)
{
lia+=‘‘+i+‘‘;
}else
{
lia+=‘<a href="javascript:vaid(0)" onclick="">‘+i+‘</a>‘;
}
}
$("#pagnation").html("<div>"+lia+"</div>");
}
}
})
}
</script>
</body>
</html>
//PHP+PDO
<?php
/**
* 文章列表接口
* 参数:kd pn pz callback
* url:zhang.com/sousuo/1.php?kd=&pn=&pz=&jsonpcallback=
* @Email:[email protected]
*/
$kd = isset($_GET[‘kd‘])?$_GET[‘kd‘]:‘‘;
$pn = isset($_GET[‘pn‘])?$_GET[‘pn‘]:1;
$pz = isset($_GET[‘pz‘])?$_GET[‘pz‘]:10;
$callback = isset($_GET[‘jsonpcallback‘])?$_GET[‘jsonpcallback‘]:$_GET[‘callback‘];
$pdo = new PDO(‘mysql:host=localhost;dbname=create‘,‘root‘,‘root‘);
$pdo->exec(‘set names utf8‘);
$position=($pn-1)*$pz;
$sql = "select * from zjy_article_cate where name_1 like ‘%$kd%‘ limit $position,$pz";
$rs = $pdo->query($sql);
$rows = $rs->fetchAll(PDO::FETCH_ASSOC);
$list[‘data‘]=$rows;
$sql = "select * from zjy_article_cate where name_1 like ‘%$kd%‘";
$rs = $pdo->query($sql);
$count = $rs->rowCount();
$list[‘total‘]=$count;
$list[‘pn‘]=$pn;
$list[‘pz‘]=$pz;
$list[‘success‘]=1;
exit($callback.‘(‘.json_encode($list).‘)‘);
?>