PHP+AJAX无刷新分页实现代码详解
PHP+AJAX无刷新分页实现代码详解,最近在看ajax 教程,就想写个简单入门的PHP+AJAX无刷新分页,我们依据ajax开发框架,代码如下:
- var http_request=false;
- function send_request(url){
- http_request=false;
-
- if(window.XMLHttpRequest){
- http_request=new XMLHttpRequest();
- if(http_request.overrideMimeType){
- http_request.overrideMimeType("text/xml");
- }
- }
- else if(window.ActiveXObject){
- try{
- http_request=new ActiveXObject("Msxml2.XMLHttp");
- }catch(e){
- try{
- http_request=new ActiveXobject("Microsoft.XMLHttp");
- }catch(e){}
- }
- }
- if(!http_request){
- window.alert("创建XMLHttp对象失败!");
- return false;
- }
- http_request.onreadystatechange=processrequest;
-
- http_request.open("GET",url,true);
- http_request.send(null);
- }
-
- function processrequest(){
- if(http_request.readyState==4){
- if(http_request.status==200){
- document.getElementById(reobj).innerHTML=http_request.responseText;
- }
- else{
- alert("您所请求的页面不正常!");
- }
- }
- }
- function dopage(obj,url){
- document.getElementById(obj).innerHTML="正在读取数据...";
- send_request(url);
- reobj=obj;
- }
内容我放在一个div中显示,当翻页动作产生时,利用AJAX更新DIV达到翻页效果这是内容显示页面代码:
- <?php
- header("Content-type: text/html;charset=GBK");
- ?>
- <html>
- <head>
- <title>ajax分页演示</title>
- <scrīpt language="javascrīpt" src="ajaxpg.js"></scrīpt>
- </head>
- <body>
- <div id="result">
- <?php
- $page=isset($_GET['page'])?intval($_GET['page']):1;
- $num=10;
- $db=mysql_connect("localhost","root","7529639");
- mysql_select_db("cr_download");
-
-
-
-
-
- $result=mysql_query("select * from cr_userinfo");
- $total=mysql_num_rows($result);
- $url='test.php';
-
- $pagenum=ceil($total/$num);
- $page=min($pagenum,$page);
- $prepg=$page-1;
- $nextpg=($page==$pagenum ? 0 : $page+1);
- $offset=($page-1)*$num;
-
- $pagenav="显示第 <B>".($total?($offset+1):0)."</B>-<B>".min($offset+10,$total)."</B> 条记录,共 $total 条记录 ";
-
- if($pagenum<=1) return false;
- $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=1');>首页</a> ";
- if($prepg) $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=$prepg');>前页</a> "; else $pagenav.=" 前页 ";
- if($nextpg) $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=$nextpg');>后页</a> "; else $pagenav.=" 后页 ";
- $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=$pagenum');>尾页</a> ";
- $pagenav.="</select> 页,共 $pagenum 页";
-
- If($page>$pagenum){
- Echo "Error : Can Not Found The page ".$page;
- Exit;
- }
- $info=mysql_query("select * from cr_userinfo limit $offset,$num");
- While($it=mysql_fetch_array($info)){
- Echo $it['username'];
- echo "<br>";
- }
- echo"<br>";
- echo $pagenav;
- ?>
- </div>
- </body>
- </html>
翻页的关键就在于翻页时调用dopage()函数,接着利用回调信息来更新div中的内容,服务器端核心代码:
- <?php
- header("Content-type: text/html;charset=GBK");
- $page=isset($_GET['page'])?intval($_GET['page']):1;
- $num=10;
- $db=mysql_connect("localhost","root","7529639");
- mysql_select_db("cr_download");
-
-
-
-
-
- $result=mysql_query("select * from cr_userinfo");
- $total=mysql_num_rows($result);
- $url='test.php';
-
- $pagenum=ceil($total/$num);
- $page=min($pagenum,$page);
- $prepg=$page-1;
- $nextpg=($page==$pagenum ? 0 : $page+1);
- $offset=($page-1)*$num;
-
- $pagenav="显示第 <B>".($total?($offset+1):0)."</B>-<B>".min($offset+10,$total)."</B> 条记录,共 $total 条记录 ";
-
- if($pagenum<=1) return false;
- $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=1');>首页</a> ";
- if($prepg) $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=$prepg');>前页</a> "; else $pagenav.=" 前页 ";
- if($nextpg) $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=$nextpg');>后页</a> "; else $pagenav.=" 后页 ";
- $pagenav.=" <a href=javascrīpt:dopage('result','$url?page=$pagenum');>尾页</a> ";
- $pagenav.="</select> 页,共 $pagenum 页";
-
- If($page>$pagenum){
- Echo "Error : Can Not Found The page ".$page;
- Exit;
- }
- $info=mysql_query("select * from cr_userinfo limit $offset,$num");
- While($it=mysql_fetch_array($info)){
- Echo $it['username'];
- echo "<br>";
- }
- echo"<br>";
- echo $pagenav;
- ?>