Realization of Paging without Refresh by php+ajax

  • 2021-07-24 10:19:41
  • OfStack

In this paper, the method of realizing non-refresh paging by php+ajax is described as an example. Share it for your reference. The specific implementation method is as follows:

This is a paging program based on the original php + js + ajax example, we detailed from database creation to js, php, html page creation to tell you how to achieve ajax paging call data method.

The specific steps are as follows:

1. Create a database

The SQL statement reads as follows:

CREATE TABLE `tb_user` (
  `id` int(10) NOT NULL auto_increment,
  `username` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ; INSERT INTO `tb_user` VALUES (1, 'aaa');
INSERT INTO `tb_user` VALUES (2, 'bbb');
INSERT INTO `tb_user` VALUES (3, 'ccc');
INSERT INTO `tb_user` VALUES (4, 'ddd');
INSERT INTO `tb_user` VALUES (5, 'eee');
INSERT INTO `tb_user` VALUES (6, 'fff');
INSERT INTO `tb_user` VALUES (7, 'ggg');
INSERT INTO `tb_user` VALUES (8, 'hhh');
INSERT INTO `tb_user` VALUES (9, '����');

2. The code for the ajaxpage. js file is as follows:

var http_request=false;
  function send_request(url){// Initialize, specify the processing function, and send the request function
    http_request=false;
    // Start initialization XMLHttpRequest Object
    if(window.XMLHttpRequest){//Mozilla Browser
     http_request=new XMLHttpRequest();
     if(http_request.overrideMimeType){// Settings MIME Category
       http_request.overrideMimeType("text/xml");
     }
    }
    else if(window.ActiveXObject){//IE Browser
     try{
      http_request=new ActiveXObject("Msxml2.XMLHttp");
     }catch(e){
      try{
      http_request=new ActiveXobject("Microsoft.XMLHttp");
      }catch(e){}
     }
    }
    if(!http_request){// Exception, object instance creation failed
     window.alert(" Create XMLHttp Object failed! ");
     return false;
    }
    http_request.onreadystatechange=processrequest;
    // Determine how to send the request, URL And whether to execute the next code synchronously
    http_request.open("GET",url,true);
    http_request.send(null);
  }
  // Functions that process returned information
  function processrequest(){
   if(http_request.readyState==4){// Judge the object state
     if(http_request.status==200){// The information has been successfully returned, and the processing of the information has started
      document.getElementById(reobj).innerHTML=http_request.responseText;
     }
     else{// Page is abnormal
      alert(" The page you requested is abnormal! ");
     }
   }
  }
  function dopage(obj,url){
   document.getElementById(obj).innerHTML=" Reading data ...";
   reobj = obj;
   send_request(url);
   }

3. The code for the php call is as follows:

<title>PHP+ajax Paging presentation </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script language="javascript" src="ajaxpage.js"></script>
<div id="result">
<?php
$terry=mysql_connect("localhost","root","")or die(" Failed to connect to database :".mysql_error());
mysql_select_db("ajaxtest",$terry);
mysql_query("set NAMES 'utf8'");
$result=mysql_query("select * from tb_user");
$total=mysql_num_rows($result) or die(mysql_error());
$page=isset($_GET['page'])?intval($_GET['page']):1;
$page_size=3;
$url='index.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='';
$pagenav.=" Display number <font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font> Record &nbsp; Altogether <b><font color='yellow'>".$total."</font></b> A record &nbsp; Now it is the first &nbsp;<b><font color='blue'>".$page."</font></b>&nbsp; Page &nbsp;";
if($page<=1)
$pagenav.="<a style=cursor:not-allowed;> Home page </a>&nbsp;";
else
$pagenav.="<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;> Home page </a>&nbsp;";
if($prepage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;> Upper 1 Page </a>&nbsp;";
else
$pagenav.="<a style=cursor:not-allowed;> Upper 1 Page </a>&nbsp;";
if($nextpage)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;> Under 1 Page </a>&nbsp;";
else
$pagenav.="<a style=cursor:not-allowed;> Under 1 Page </a>&nbsp;";
if($pagenum)
$pagenav.="<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;> End page </a>&nbsp;";
else
$pagenav.="<a style=cursor:not-allowed;> End page </a>&nbsp;";
$pagenav.=" Altogether ".$pagenum." Page ";
if($page>$pagenum){
    echo "error: This page is not available ".$page;
    exit();
}
?>
<table align="center" border="2" width="300">
  <tr bgcolor="#cccccc" align="center">
    <td> User name </td>
    <td> User password </td>
  </tr>
<?php
$info=mysql_query("select * from tb_user order by id desc limit $pageset,$page_size");
while($array=mysql_fetch_array($info)){
?>
  <tr align="center">
    <td><?php echo $array['id'];?></td>
    <td><?php echo $array['username'];?></td>
  </tr>
<?php   
}
?>
</table>
<?php
echo "<p align=center>$pagenav</p>";
?>
</div>

I hope this article is helpful to everyone's PHP programming.


Related articles: