PHP + AJAX implementation of refreshing pagination

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces the implementation method of PHP + AJAX refreshing pagination. For more information about the code of PHP + AJAX refreshing pagination, see the ajax tutorial, I want to write a simple entry-level PHP + AJAX without refreshing pages. based on the ajax development framework, the code is as follows:

Var http_request = false; function send_request (url) {// initialization, specifies the processing function, and the request sending function http_request = false; // starts to initialize the XMLHttpRequest object if (window. XMLHttpRequest) {// Mozilla http_request = new XMLHttpRequest (); if (http_request.overrideMimeType) {// Set the 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 the object instance window. alert ("failed to create the XMLHttp object! "); Return false;} http_request.onreadystatechange = processrequest; // Determine the request sending method, URL, and whether to synchronously execute the following code http_request.open (" GET ", url, true ); http_request.send (null);} // function processrequest () {if (http_request.readyState = 4) {// judge the object status if (http_request.status = 200) {// The information has been returned successfully. start to process the information document. getElementById (reobj ). innerHTML = http_request.responseText;} else {// The page is not normal alert ("the page you requested is not normal! ") ;}} Function dopage (obj, url) {document. getElementById (obj ). innerHTML = "reading data... "; send_request (url); reobj = obj ;}

I put the content in a p display. when the page flip action is generated, I use AJAX to update the DIV to achieve the page flip effect. this is the page code for displaying the content:

<? Phpheader ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters?>Ajax paging demonstration
 

<? Php $ page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. if there is no page, the page number is 1. $ Num = 10; // 10 pieces of data are displayed on each page. $ db = mysql_connect ("localhost", "root", "7529639 "); // create a database to connect to mysql_select_db ("cr_download"); // select the database to be operated on/* First, we need to obtain the actual amount of data in the database to determine the specific page to be divided, the specific formula is that the total database is divided by the number of entries displayed on each page, more than one. That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter. */$ Result = mysql_query ("select * from cr_userinfo"); $ total = mysql_num_rows ($ result); // query all data $ url = 'Test. php '; // Obtain the URL of the current page // calculate the page number $ pagenum = ceil ($ total/$ num); // obtain the total page number, also the last page $ page = min ($ pagenum, $ page); // Get the homepage $ prepg = $ page-1; // $ offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. for example, if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10. // Start pagination navigation bar code: $ pagenav = "display". ($ Total? ($ Offset + 1): 0 )."-". Min ($ offset + 10, $ total )."Records, total $ total records "; // if there is only one page, the function will jump out: if ($ pagenum <= 1) return false; $ pagenav. = "homepage"; if ($ prepg) $ pagenav. = "previous page"; else $ pagenav. = "previous page"; if ($ nextpg) $ pagenav. = ""; else $ pagenav. = "back page"; $ pagenav. = "Last page"; $ pagenav. ="Page, total $ pagenum page "; // If the incoming page number parameter is greater than the total page number, the Error message If ($ page> $ pagenum) {Echo" Error: can Not Found The page ". $ page; Exit;} $ info = mysql_query ("select * from cr_userinfo limit $ offset, $ num "); // obtain the data to be displayed for the corresponding page number While ($ it = mysql_fetch_array ($ info) {Echo $ it ['username']; echo"
";}// Display data echo"
"; Echo $ pagenav; // output paging navigation?>

The key to page turning is to call the dopage () function during page turning, and then use the callback information to update the content in p. The core code on the server side is as follows:

<? Phpheader ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters $ page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. if there is no page, the page number is 1. $ Num = 10; // 10 pieces of data are displayed on each page. $ db = mysql_connect ("localhost", "root", "7529639 "); // create a database to connect to mysql_select_db ("cr_download"); // select the database to be operated on/* First, we need to obtain the actual amount of data in the database to determine the specific page to be divided, the specific formula is that the total database is divided by the number of entries displayed on each page, more than one. That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter. */$ Result = mysql_query ("select * from cr_userinfo"); $ total = mysql_num_rows ($ result); // query all data $ url = 'Test. php '; // Obtain the URL of the current page // calculate the page number $ pagenum = ceil ($ total/$ num); // obtain the total page number, also the last page $ page = min ($ pagenum, $ page); // Get the homepage $ prepg = $ page-1; // $ offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. for example, if the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10. // Start pagination navigation bar code: $ pagenav = "display". ($ Total? ($ Offset + 1): 0 )."-". Min ($ offset + 10, $ total )."Records, total $ total records "; // if there is only one page, the function will jump out: if ($ pagenum <= 1) return false; $ pagenav. = "homepage"; if ($ prepg) $ pagenav. = "previous page"; else $ pagenav. = "previous page"; if ($ nextpg) $ pagenav. = ""; else $ pagenav. = "back page"; $ pagenav. = "Last page"; $ pagenav. ="Page, total $ pagenum page "; // If the incoming page number parameter is greater than the total page number, the Error message If ($ page> $ pagenum) {Echo" Error: can Not Found The page ". $ page; Exit;} $ info = mysql_query ("select * from cr_userinfo limit $ offset, $ num "); // obtain the data to be displayed for the corresponding page number While ($ it = mysql_fetch_array ($ info) {Echo $ it ['username']; echo"
";}// Display data echo"
"; Echo $ pagenav; // output paging navigation?>

The above explains in detail the implementation of PHP + AJAX without refreshing pages, hoping to help you learn php programming.

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.