"jquery, ajax,php" load more instances

Source: Internet
Author: User

Jquery

$ (function () {//Initialize GetData (0); var index = 1;$ ("#more"). Click (function () {getData (index) index = index + 1;}) var cur_page = 1;var Total, total_page, Page_size;//ajax interactive function getData (pageIndex) {$.ajax ({type: "POST", url: " Test.php ", data: {" PageIndex ": PageIndex},//pass parameters as the background to return page numbers by datatype:" JSON ",//expected data returned as Jsonbeforesend:function () {$ ("#more"). Text ("Loading ...")},success:function (JSON) {////After successfully fetching the data, return the JSON object, which is a JSON name to my understanding is json={... }$ ("#more"). Text ("Load more ..."); total = Json.total; Gets the total attribute value in json pageSize = json.pagesize; Gets the value of the PageSize property in json totalpage = Json.totalpage;var list = json.list; The list in JSON is an array of var li = ""; $.each (list, function (index, content) {//Traversal list array, index is subscript, content is the value of this subscript Li + = "<ul& Gt;<li class= ' question ' > "+ content[' question '] +" </li><li class= ' answer ' > "+ content[' answer '] +" & Lt;/li></ul> ";}); $ ("#list"). Append (LI), if (index >= totalpage) {$ ("#more"). Text ("No"). CSS ("Background", "#555"). Unbind ("click"); Unbind the Click event}},error: function () {alert ("Load Error");}})}) 

Php

<?phpinclude_once (' conn.php '); $page = Intval ($_post[' pageIndex ');//Receive data from the foreground if (!empty ($page)) {$result = Mysql_ Query ("SELECT ID from Test1"), $total = Mysql_num_rows ($result);//total record number $pagesize = 3; Display number per page $totalpage = ceil ($total/$pageSize); Total pages $startpage = $page * $pageSize; $arr [' totals '] = $total; $arr [' pageSize '] = $pageSize; $arr [' totalpage '] = $totalPage; $ query = mysql_query ("Select Id,question,answer from Test1 ORDER by ID ASC limit $startPage, $pageSize"); while ($row =mysql_f Etch_array ($query)) {//Get all data rows $arr[' list ' [] = array (' id ' = + $row [' ID '],//assigns the value of the ID field in the row to the id ' question ' = = $row [' Question '], ' answer ' = $row [' answer '],);} mysql_query (' SET NAMES UTF8 '), header ("Content-type:text/html;charset = Utf-8"), Echo Json_encode ($arr);//Convert to JSON format, The character format output here is not relevant to the foreground}?>

  

"jquery, ajax,php" load more instances

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.