Php Ajax paging simple application example

Source: Internet
Author: User

This article uses Ajax paging to briefly describe how to use php and ajax to implement the function of refreshing data pages. If you need it, please refer to it.

Simple mysql Data Table Structure

The Code is as follows: Copy code

Create table messages
(
Msg_id int primary key AUTO_INCREMENT,
Message VARCHAR (1, 150)
);

JavaScript code

In the previous section of ajax, jquery was used for processing.

The Code is as follows: Copy code

<Script type = "text/javascript" src = "http://ajax.googleapis.com/
Ajax/libs/jquery/1.4.2/jquery. min. js "> </script>
<Script type = "text/javascript">
$ (Document). ready (function ()
{

Function loading_show ()
{
Certificate ('{loading'{.html ("}

Function loading_hide ()
{
$ ('# Loading'). fadeOut ();
}

Function loadData (page)
{
Loading_show ();
$. Ajax
({
Type: "POST ",
Url: "load_data.php ",
Data: "page =" + page,
Success: function (msg)
{
$ ("# Container"). ajaxComplete (function (event, request, settings)
{
Loading_hide ();
$ ("# Container" cmd.html (msg );
});
}
});
}
LoadData (1); // For first time page load default results
$ ('# Container. pagination li. activity'). live ('click', function (){
Var page = $ (this). attr ('P ');
LoadData (page );
});
});

</Script>

Load_data.php

Here we get the data sent by ajax and query the mysql return information through php.

The Code is as follows: Copy code

<? Php
If ($ _ POST ['page'])
{
$ Page = $ _ POST ['page'];
$ Cur_page = $ page;
$ Page-= 1;
$ Per_page = 15; // Per page records
$ Previous_btn = true;
$ Next_btn = true;
$ First_btn = true;
$ Last_btn = true;
$ Start = $ page * $ per_page;
Include "db. php ";
$ Query_pag_data = "SELECT msg_id, message from messages LIMIT $ start, $ per_page ";
$ Result_pag_data = mysql_query ($ query_pag_data) or die ('mysql error'. mysql_error ());
$ Msg = "";
While ($ row = mysql_fetch_array ($ result_pag_data ))
{
$ Htmlmsg = htmlentities ($ row ['message']); // HTML entries filter
$ Msg. = "<li> <B>". $ row ['msg _ id']. "</B>". $ htmlmsg. "</li> ";
}
$ Msg = "<div class = 'data'> <ul>". $ msg. "</ul> </div>"; // Content for data
/* ----- Total count ---*/
$ Query_pag_num = "select count (*) AS count FROM messages"; // Total records
$ Result_pag_num = mysql_query ($ query_pag_num );
$ Row = mysql_fetch_array ($ result_pag_num );
$ Count = $ row ['Count'];
$ No_of_paginations = ceil ($ count/$ per_page );
/* ----- Calculating the starting and endign values for the loop -----*/

// Some Code. Available in download script

}
?>

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.