Php + ajax implements the method of refreshing pagination, ajax pagination

Source: Internet
Author: User

Php + ajax implements the method of refreshing pagination, ajax pagination

The example in this article describes how php + ajax implements a non-refreshing paging method. Share it with you for your reference. The specific implementation method is as follows:

This is a paging program instance based on the original ecology of php + js + ajax. We will create a detailed example from the database to js, php, the creation of html pages tells you how to call data by ajax page.

The procedure is as follows:

1. Create a database

The SQL statement is as follows:

Copy codeThe Code is 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, '�� ');

II. The ajaxpage. js file code is as follows:

Copy codeThe Code is as follows: var http_request = false;
Function send_request (url) {// initialization, specifying the processing function and sending the request function
Http_request = false;
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType) {// sets 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. An error occurred while creating the object instance.
Window. alert ("An error occurred while creating the XMLHttp object! ");
Return false;
}
Http_request.onreadystatechange = processrequest;
// Determine the request sending method, URL, and whether to execute the following code synchronously
Http_request.open ("GET", url, true );
Http_request.send (null );
}
// Function for processing the returned information
Function processrequest (){
If (http_request.readyState = 4) {// judge the object status
If (http_request.status = 200) {// The message is returned successfully. Start to process the information.
Document. getElementById (reobj). innerHTML = http_request.responseText;
}
Else {// The 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 );
}
Iii. php call code:

Copy codeThe Code is as follows: <title> PHP + ajax paging demonstration </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 the 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 no. <font color = 'red'>". ($ total? ($ Pageset + 1): 0 ). "-". min ($ pageset + 5, $ total ). "</font> total records <B> <font color = 'yellow'> ". $ total. "</font> </B> the record number is <B> <font color = 'blue'> ". $ page. "</font> </B> page ";
If ($ page <= 1)
$ Pagenav. = "<a style = cursor: not-allowed;> homepage </a> ";
Else
$ Pagenav. = "<a onclick = javascript: dopage ('result', '$ url? Page = 1') style = cursor: pointer;> homepage </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;> previous page </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 page </a> ";
Else
$ Pagenav. = "<a style = cursor: not-allowed;> last page </a> ";
$ Pagenav. = "Total". $ pagenum. "Page ";
If ($ page> $ pagenum ){
Echo "error: This page does not exist". $ 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 will help you with PHP programming.


Why does PHP + ajax not refresh pages, but the Select All button becomes invalid? Solution

The onchange event of the select-all button must be added after the list data is obtained through ajax and the onchange event. Here, the checkbox in the list can be obtained. If js is called directly on the page, the scope is out, and js cannot get the checkbox in the list.

Who told me about the principle of ajax + php refreshing pagination?

Ajax is a js script. Or use ajax in jquery
If you want to verify the user name. You can add onchange = "call function" in your input attribute"
Calling a function is ajax code.
Ajax completes the work in this way,
First, send the request to your specified page, such as abc. php (with the post or get you set ),
After abc. php responds successfully, execute the code in it to process the parameters you passed. If this user exists. Then, if a value or function does not exist, it will return a value or function. You need to set it yourself.
Ajax then processes the returned value or function, for example, displaying it, or calling the function to disable the submit button.

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.