Php+ajax method of realizing non-refreshing paging _php skills

Source: Internet
Author: User

The example in this article describes a method for Php+ajax implementation without refreshing paging. Share to everyone for your reference. The implementation methods are as follows:

This is based on the original ecology of the PHP +js +ajax of the page program instance, we detailed from the database created to the js,php,html page creation to tell you how to implement the Ajax paging data method.

The specific steps are as follows:

First, create a database

The SQL statement is as follows:

Copy Code code as follows:
CREATE TABLE ' Tb_user ' (
' ID ' int (a) not NULL auto_increment,
' username ' varchar 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, ');

Second, the Ajaxpage.js file code is as follows:

Copy Code code as follows:
var Http_request=false;
function send_request (URL) {//initialization, specifying handler functions, sending requested functions
Http_request=false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla browser
Http_request=new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set 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, failed to create object instance
Window.alert ("Create XMLHTTP object failed!") ");
return false;
}
Http_request.onreadystatechange=processrequest;
Determine how to send the request, the URL, and whether to execute the next code synchronously
Http_request.open ("Get", url,true);
Http_request.send (NULL);
}
Functions that process return information
function ProcessRequest () {
if (http_request.readystate==4) {//Judge object state
if (http_request.status==200) {//information returned successfully, start processing information
document.getElementById (reobj). Innerhtml=http_request.responsetext;
}
else{//page is not normal
Alert ("The page you requested is not normal!") ");
}
}
}
function Dopage (obj,url) {
document.getElementById (obj). innerhtml= "reading data ...";
Reobj = obj;
Send_request (URL);
}

Three, the PHP calling code is as follows:

Copy Code code as follows:
<title>php+ajax Pagination Demo </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 ("Connection Database failed:". 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. = "Show <font color= ' Red ' >". ($total? ($pageset + 1): 0). " -". Min ($pageset +5, $total)." </font> records a total of <b><font color= ' yellow ' > '. $total. " </font></b> records are now <b><font color= ' Blue ' > '. $page. " </font></b> page ";
if ($page <=1)
$pagenav. = "<a style=cursor:not-allowed;> home </a>";
Else
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page=1 ') style=cursor:pointer;> home </a>";
if ($prepage)
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page= $prepage ') style=cursor:pointer;> previous page </a> ";
Else
$pagenav. = "<a style=cursor:not-allowed;> prev </a>";
if ($nextpage)
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page= $nextpage ') style=cursor:pointer;> next page </a > ";
Else
$pagenav. = "<a style=cursor:not-allowed;> next page </a>";
if ($pagenum)
$pagenav. = "<a onclick=javascript:dopage (' result ', ' $url? page= $pagenum ') style=cursor:pointer;> last </a>" ;
Else
$pagenav. = "<a style=cursor:not-allowed;> last </a>";
$pagenav. = "Total". $pagenum. " Page ";
if ($page > $pagenum) {
echo "Error: no this page". $page;
Exit ();
}
?>
<table align= "center" border= "2" width= ">"
<tr bgcolor= "#cccccc" align= "Center" >
<td> User name </td>
<td> User Password </td>
</tr>
<?php
$info =mysql_query ("SELECT * from Tb_user" desc limit $pageset, $page _size ");
while ($array =mysql_fetch_array ($info)) {
?>
&LT;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 will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.