ajax 는 두 페이지 의 페이지 분할 기능 을 완벽 하 게 실현 하 는 인 스 턴 스 코드 입 니 다.

페이지 별 첫 페이지

<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<h1 align="center"> </h>
<br/>
<script src="jquery-1.4.2.min.js"></script>
<script>
var page='';
  function init(page){
  document.getElementById("tables").innerHTML='';
   var xhr;
   if(window.XMLHttpRequest){
 xhr = new XMLHttpRequest();
}else if(window.ActiveXObject){
 xhr =new ActiveXObject("Microsoft.XMLHTTP")
}
var url="fenye.php";
 xhr.open("POST",url,true);
 xhr.onreadystatechange=callback;
 xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 if(page){
 xhr.send("page="+page);
 }
 else {xhr.send("page=1");}
 var content;
 function callback(){
   if(xhr.readyState==4){
     if(xhr.status==200){
      var json =eval('('+xhr.responseText+')');
   //alert(xhr.responseText);
  var fenye=json.str;
 //  alert(fenye);
     document.getElementById('div').innerHTML=fenye;
  content="<th>ID</th><th> </th><th> </th><th> </th><th>ISBN </th><th> </th><th> </th>";
  for(var i=0;i<json.info.length;i++){
  content+="<tr><td>"+json.info[i].id+"</td><td>"+json.info[i].name+"</td><td>"+json.info[i].author+"</td><td>"+json.info[i].publisher+"</td><td>"+json.info[i].isbn+"</td><td>"+json.info[i].type+"</td><td>"+json.info[i].price+"</tr>";
  document.getElementById("tables").innerHTML=content;
  }
    // alert(fenye);
     }
   }
   }
 }

</script>


<body onLoad="init()">
 <h3 align="center">jquery $.ajax </h3>
<table id="tables" style=" width:500px; height:100px; text-align:center" align="center" border="5" bordercolor="#FF6600">
<th>ID</th><th> </th><th> </th><th> </th><th>ISBN </th><th> </th><th> </th>
</table>
<table align="center" style="margin-top:15px">
 <tr><td>
 <td style="colspan:3;height:20">
 <div id="div" style="position:absolute; left: 447px; top: 218px; width: 411px; height: 22px;"></div>
 </td>
</tr>
</table>
<br />
</body>

 
페이지 별 php 에센스 코드

<?php
    //
  $link=mysql_connect('localhost','root','') or die(" ");
  mysql_select_db('books',$link) or die(" !");
  //
  $pagesize = 2;
  //
  $sql = "select count(*) as total_rows from books";
  $result = mysql_query($sql);
  $total_rows = mysql_fetch_array($result);
  //
  $pages = ceil($total_rows[0]/$pagesize);
  //
  $page = $_POST['page'];
  $strtext = " ".$page." "." ".$pages." "." ".$total_rows[0]." ";
  //var_dump($str);
  // ,
  $offset = $pagesize*($page-1);
  $sql = "select * from books limit $offset,$pagesize";
  mysql_query("set names utf8");
        $res=mysql_query($sql);

   $rows=array();
  while($row=mysql_fetch_assoc($res)){
    $rows[]=$row;
   }
  $pageInfo = $rows;
  //echo json_encode($pageInfo);
  //var_dump($pageInfo);
  // ,
  $first=1;
  $prev=$page-1;
  $next=$page+1;
  $last=$pages;

  //
      $first_a = "<a onclick='init(".$first.")' href='#'><img src='3.jpg' width='50px;' height='20px;'/></a>";
  if($page>1){
   $prev_a = "<a onclick='init(".$prev.")' href='#'><img src='1.jpg' width='50px;' height='20px;'/></a>";
  }
  if($page<$pages){
   $next_a = "<a onclick='init(".$next.")' href='#'><img src='2.jpg' width='50px;' height='20px;'/></a>";
  }
  $last_a = "<a onclick='init(".$last.")' href='#'><img src='4.jpg' width='50px;' height='20px;'/></a>";
  @$str = $strtext.$first_a.$prev_a.$next_a.$last_a;
  //var_dump($str);
  $info = array('str'=>$str,'info'=>$pageInfo);
  echo json_encode($info);

좋은 웹페이지 즐겨찾기