????:本文實(shí)例講述了php+ajax無(wú)刷新分頁(yè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:ajax_page_show_userinfo.php頁(yè)面如下:<meta 'Content:text/html;charset=utf-8'></meta> <title>ajax分頁(yè)演示</title> <script la
本文實(shí)例講述了php+ajax無(wú)刷新分頁(yè)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
ajax_page_show_userinfo.php頁(yè)面如下:
<meta 'Content:text/html;charset=utf-8'></meta> <title>ajax分頁(yè)演示</title> <script language="javascript" src="js/ajaxpage.js"></script> <div id="result"> <?php $db=mysql_connect("localhost","root","123456"); mysql_SELECT_db("register"); mysql_query("set names 'utf-8'"); $result=mysql_query("SELECT * from user"); $total=mysql_num_rows($result) or die(mysql_error()); $page=isset($_GET['page'])?intval($_GET['page']):1; $page_size=5; $url='ajax_page_show_userinfo.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.="顯示第".($total?($pageset+1):0)."-".min($pageset+5,$total)."記錄 共<b>".$total."</b>條記錄 現(xiàn)在是第 <b>".$page."</b> 頁(yè) "; if($page<=1) $pagenav.="<a style=cursor:not-allowed;>首頁(yè)</a> "; else $pagenav.="<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;>首頁(yè)</a> "; if($prepage) $pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;>上一頁(yè)</a> "; else $pagenav.="<a style=cursor:not-allowed;>上一頁(yè)</a> "; if($nextpage) $pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;>下一頁(yè)</a> "; else $pagenav.="<a style=cursor:not-allowed;>下一頁(yè)</a> "; if($pagenum) $pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;>尾頁(yè)</a> "; else $pagenav.="<a style=cursor:not-allowed;>尾頁(yè)</a> "; $pagenav.="共".$pagenum."頁(yè)"; if($page>$pagenum){ echo "error:沒(méi)有此頁(yè)".$page; exit(); } ?> <table align="center" border="2" width="300"> <tr bgcolor="#cccccc" align="center"> <td>用戶名</td> <td>用戶密碼</td> </tr> <?php $info=mysql_query("SELECT * from user limit $pageset,$page_size"); while($array=mysql_fetch_array($info)){ ?> <tr align="center"> <td><?php echo $array['username'];?></td> <td><?php echo $array['password'];?></td> </tr> <?php } ?> </table> <?php echo "<p align=center>$pagenav</p>"; ?> </div>
js文件下的ajaxpage.js:
var http_request=false; function send_request(url){//初始化,指定處理函數(shù),發(fā)送請(qǐng)求的函數(shù) http_request=false; //開(kāi)始初始化XMLHttpRequest對(duì)象 if(window.XMLHttpRequest){//Mozilla瀏覽器 http_request=new XMLHttpRequest(); if(http_request.overrideMimeType){//設(shè)置MIME類別 http_request.overrideMimeType("text/xml"); } } else if(window.ActiveXObject){//IE瀏覽器 try{ http_request=new ActiveXObject("Msxml2.XMLHttp"); }catch(e){ try{ http_request=new ActiveXobject("Microsoft.XMLHttp"); }catch(e){} } } if(!http_request){//異常,創(chuàng)建對(duì)象實(shí)例失敗 window.alert("創(chuàng)建XMLHttp對(duì)象失?。?quot;); return false; } http_request.onreadystatechange=processrequest; //確定發(fā)送請(qǐng)求方式,URL,及是否同步執(zhí)行下段代碼 http_request.open("GET",url,true); http_request.send(null); } //處理返回信息的函數(shù) function processrequest(){ if(http_request.readyState==4){//判斷對(duì)象狀態(tài) if(http_request.status==200){//信息已成功返回,開(kāi)始處理信息 document.getElementByIdx(reobj).innerHTML=http_request.responseText; } else{//頁(yè)面不正常 alert("您所請(qǐng)求的頁(yè)面不正常!"); } } } function dopage(obj,url){ //document.getElementByIdx(obj).innerHTML="正在讀取數(shù)據(jù)..."; send_request(url); reobj=obj; }
更多關(guān)于php+ajax無(wú)刷新分頁(yè)實(shí)例詳解請(qǐng)關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!