php+ajax实现无刷新分页的方法
本文实例讲述了php+ajax实现无刷新分页的方法。分享给大家供大家参考。具体实现方法如下:
这是一款基于原生态的php+js+ajax的分页程序实例,我们详细的从数据库创建到js,php,html页面的创建来告诉你如何实现ajax分页调用数据的方法。
具体步骤如下:
一、创建数据库
SQL语句如下:
`id`int(10)NOTNULLauto_increment,
`username`varchar(50)NOTNULL,
PRIMARYKEY (`id`)
)ENGINE=MyISAM DEFAULTCHARSET=utf8AUTO_INCREMENT=10;
INSERTINTO`tb_user`VALUES(1,"aaa");
INSERTINTO`tb_user`VALUES(2,"bbb");
INSERTINTO`tb_user`VALUES(3,"ccc");
INSERTINTO`tb_user`VALUES(4,"ddd");
INSERTINTO`tb_user`VALUES(5,"eee");
INSERTINTO`tb_user`VALUES(6,"fff");
INSERTINTO`tb_user`VALUES(7,"ggg");
INSERTINTO`tb_user`VALUES(8,"hhh");
INSERTINTO`tb_user`VALUES(9,"����");
二、ajaxpage.js文件代码如下:
functionsend_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest){//Mozilla浏览器
http_request=newXMLHttpRequest();
if(http_request.overrideMimeType){//设置MIME类别
http_request.overrideMimeType("text/xml");
}
}
elseif(window.ActiveXObject){//IE浏览器
try{
http_request=newActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=newActiveXobject("Microsoft.XMLHttp");
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert("创建XMLHttp对象失败!");
returnfalse;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,URL,及是否同步执行下段代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
functionprocessrequest(){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getElementById(reobj).innerHTML=http_request.responseText;
}
else{//页面不正常
alert("您所请求的页面不正常!");
}
}
}
functiondopage(obj,url){
document.getElementById(obj).innerHTML="正在读取数据...";
reobj=obj;
send_request(url);
}
三、php调用代码如下:
希望本文所述对大家的PHP程序设计有所帮助。
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scriptlanguage="javascript"src="ajaxpage.js"></script>
<divid="result">
<?php
$terry=mysql_connect("localhost","root","")ordie("连接数据库失败:".mysql_error());
mysql_select_db("ajaxtest",$terry);
mysql_query("setNAMES"utf8"");
$result=mysql_query("select*fromtb_user");
$total=mysql_num_rows($result)ordie(mysql_error());
$page=isset($_GET["page"])?intval($_GET["page"]):1;
$page_size=3;
$url="index.php";
$pagenum=ceil($total/$page_size);
$page=min($pagenum,$page);
$prepage=$page-1;
$nextpage=($page==$pagenum?0:$page+1);
$pageset=($page-1)*$page_size;
$pagenav="";
$pagenav.="显示第<fontcolor="red">".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>记录 共<b><fontcolor="yellow">".$total."</font></b>条记录 现在是第 <b><fontcolor="blue">".$page."</font></b> 页 ";
if($page<=1)
$pagenav.="<astyle=cursor:not-allowed;>首页</a> ";
else
$pagenav.="<aonclick=javascript:dopage("result","$url?page=1")style=cursor:pointer;>首页</a> ";
if($prepage)
$pagenav.="<aonclick=javascript:dopage("result","$url?page=$prepage")style=cursor:pointer;>上一页</a> ";
else
$pagenav.="<astyle=cursor:not-allowed;>上一页</a> ";
if($nextpage)
$pagenav.="<aonclick=javascript:dopage("result","$url?page=$nextpage")style=cursor:pointer;>下一页</a> ";
else
$pagenav.="<astyle=cursor:not-allowed;>下一页</a> ";
if($pagenum)
$pagenav.="<aonclick=javascript:dopage("result","$url?page=$pagenum")style=cursor:pointer;>尾页</a> ";
else
$pagenav.="<astyle=cursor:not-allowed;>尾页</a> ";
$pagenav.="共".$pagenum."页";
if($page>$pagenum){
echo"error:没有此页".$page;
exit();
}
?>
<tablealign="center"border="2"width="300">
<trbgcolor="#cccccc"align="center">
<td>用户名</td>
<td>用户密码</td>
</tr>
<?php
$info=mysql_query("select*fromtb_userorderbyiddesclimit$pageset,$page_size");
while($array=mysql_fetch_array($info)){
?>
<tralign="center">
<td><?phpecho$array["id"];?></td>
<td><?phpecho$array["username"];?></td>
</tr>
<?php
}
?>
</table>
<?php
echo"<palign=center>$pagenav</p>";
?>
</div>
相关文章