ajax实现分页

来源:互联网 发布:新浪期货软件下载 编辑:程序博客网 时间:2024/05/16 09:38
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>
原创粉丝点击