Example code for ajax to perfectly implement the paging function of two webpages

Source: Internet
Author: User

Paging Homepage

Copy codeThe Code is as follows: <meta http-equiv = "Content-Type" content = "text/html; charset = utf8"/>
<H1 align = "center"> pagination of martial arts novels <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> name </th> <th> author </th> <th> press </th> <th> ISBN No. </th> <th> type </th> <th> price </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 implementation $. ajax pagination <Table id = "tables" style = "width: 500px; height: 100px; text-align: center "align =" center "border =" 5 "bordercolor =" # FF6600 ">
<Th> ID </th> <th> name </th> <th> author </th> <th> press </th> <th> ISBN number </th>> <th> type </th> <th> price </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>

Paging php essential code

Copy codeThe Code is as follows: <? Php
// Process data at the command model layer
$ Link = mysql_connect ('localhost', 'root', '') or die (" failed ");
Mysql_select_db ('books ', $ link) or die ("An error occurred while connecting to the database! ");
// Number of records displayed per page
$ Pagesize = 2;
// Obtain the total number of records
$ SQL = "select count (*) as total_rows from books ";
$ Result = mysql_query ($ SQL );
$ Total_rows = mysql_fetch_array ($ result );
// Calculate the total number of page numbers
$ Pages = ceil ($ total_rows [0]/$ pagesize );
// Current page
$ Page = $ _ POST ['page'];
$ Strtext = "current section". $ page. "page". "Total". $ pages. "page". "Total". $ total_rows [0]. "record ";
// Var_dump ($ str );
// Next, I will find the corresponding data based on the page number currently clicked
$ 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 );
// Get the data link and return it
$ First = 1;
$ Prev = $ page-1;
$ Next = $ page + 1;
$ Last = $ pages;

// Display data at the command view layer
$ First_a = "<a onclick = 'init (". $ first. ") 'href = '#'> </a> ";
If ($ page> 1 ){
$ Prev_a = "<a onclick = 'init (". $ prev. ") 'href = '#'> </a> ";
}
If ($ page <$ pages ){
$ Next_a = "<a onclick = 'init (". $ next. ") 'href = '#'> </a> ";
}
$ Last_a = "<a onclick = 'init (". $ last. ") 'href = '#'> </a> ";
@ $ Str = $ strtext. $ first_a. $ prev_a. $ next_a. $ last_a;
// Var_dump ($ str );
$ Info = array ('str' => $ str, 'info' => $ pageInfo );
Echo json_encode ($ info );

Related Article

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.