ajax.js代码: var HTTP_Request=false; function send_request(url) { HTTP_Request=false; //开始初始化XMLHttp对象 if(window.XMLHttpRequest){//MOILLA浏览器 HTTP_Request=new XMLHttpRequest(); if(HTTP_Request.overrideMimeType){ 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){ window.alert("创建XMLHttp对象失败"); return false; } HTTP_Request.onreadystatechange=processrequest; //确定发送请求方式,URL,及是否同步执行下段代码 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; } myajax.PHP代码: <?PHP header("Content-type:text/html;charset=gb2312"); ?> <html> <head> <title>ajax分页</title> <script src="ajax.js"></script> <style> .d{font-size:13px} table{font-size:14px} a{ text-decoration:none} </style> </head> <body> <div id="result" class="d"> <?PHP $page=isset($_GET["page"])?intval($_GET["page"]):1; $num=3; //每页显示3条数据 $link=MysqL_connect("localhost","root","8821") or die("数据库连接失败".MysqL_error()); MysqL_select_db("page") or die("数据库选择失败".MysqL_error()); MysqL_query("set names 'gb2312'"); $result=MysqL_query("select * from student"); $total=MysqL_num_rows($result);//查询所有数据 $url="myajax.PHP"; //获取本页url //页码计算 $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+3,$total)."<b>条记录,共".$total."条记录$nbsp;"; //如果只有一页则跳出函数 if($pagenum<=1) return false; $pagenav.="<a href=javascript:dopage('result','$url?page=1');>首页</a>--"; if($prepg)$pagenav.="<a href=javascript:dopage('result','$url?page=$prepg');>前页--</a>";else $pagenav.="前页--"; if($nextpg)$pagenav.="<a href=javascript:dopage('result','$url?page=$nextpg');>后页--</a>";else $pagenav.="后页--"; $pagenav.="<a href=javascript:dopage('result','$url?page=$pagenum');>尾页</a>"; $pagenav.="</select>共".$pagenum."页"; if($page>$pagenum){ //假如传入的页面参数大于总页数 则现实错误信息 echo "can not found the page".$page; exit; } $info=MysqL_query("select * from student limit $offset,$num"); echo "<table border=3>"; while($it=MysqL_fetch_array($info)){ echo "<tr>"; echo "<td>".$it["id"]."</td>"; echo "<td>".$it["name"]."</td>"; echo "<td>".$it["age"]."</td>"; echo "<td>".$it["email"]."</td>"; echo "</tr>"; } echo "</table>"; echo "<br>"; echo $pagenav; ?> </div> </body> </html>