Table of Contents
Ajax无刷新分页效果
Home Web Front-end JS Tutorial Simple implementation of Ajax non-refresh paging effect

Simple implementation of Ajax non-refresh paging effect

Jan 01, 2018 pm 07:40 PM
ajax Pagination refresh

This article mainly introduces in detail the code to simply implement the Ajax non-refresh paging effect. It has certain reference and value for learning ajax. Friends who are interested in ajax can refer to it

Ajax No refresh paging effect, the following code implements


<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Ajax无刷新分页效果</title>

 <script type="text/javascript">
 function showpage(url) {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function () {
  if (xhr.readyState == 4) {
   document.getElementById("result").innerHTML = xhr.responseText;
  }
  }

  xhr.open(&#39;get&#39;,url);
  xhr.send(null);
 }

 window.onload = function () {
  showpage(&#39;page.php&#39;);
 }
 </script>
</head>
<body>
  <h2 id="Ajax无刷新分页效果">Ajax无刷新分页效果</h2>
  <p id="result"></p>
</body>
</html>
Copy after login


The paging code found online, can be used for personal testing~


/*
* Created on 2011-07-28
* Author : LKK , http://lianq.net
* 使用方法:
require_once(&#39;mypage.php&#39;);
$result=mysql_query("select * from mytable", $myconn);
$total=mysql_num_rows($result); //取得信息总数
pagepide($total,10); //调用分页函数

//数据库操作
$result=mysql_query("select * from mytable limit $sqlfirst,$shownu", $myconn);
while($row=mysql_fetch_array($result)){
...您的操作
}
echo $pagecon; //输出分页导航内容
*/

//if(!function_exists("pagepide")){
#$total 信息总数
#$shownu 显示数量,默认20
#$url 本页链接
 function pagepide($total,$shownu=20,$url=&#39;&#39;){

#$page 当前页码
#$sqlfirst mysql数据库起始项
#$pagecon 分页导航内容
 global $page,$sqlfirst,$pagecon,$_SERVER;
 $GLOBALS["shownu"]=$shownu;

 if(isset($_GET[&#39;page&#39;])){
  $page=$_GET[&#39;page&#39;];
 }else $page=1;

#如果$url使用默认,即空值,则赋值为本页URL
 if(!$url){ $url=$_SERVER["REQUEST_URI"];}

#URL分析
 $parse_url=parse_url($url);
 @$url_query=$parse_url["query"]; //取出在问号?之后内容
 if($url_query){
  $url_query=preg_replace("/(&?)(page=$page)/","",$url_query);
  $url = str_replace($parse_url["query"],$url_query,$url);
  if($url_query){
  $url .= "&page";
  }else $url .= "page";
 }else $url .= "?page";

#页码计算
 $lastpg=ceil($total/$shownu); //最后页,总页数
 $page=min($lastpg,$page);
 $prepg=$page-1; //上一页
 $nextpg=($page==$lastpg ? 0 : $page+1); //下一页
 $sqlfirst=($page-1)*$shownu;

#开始分页导航内容
 $pagecon = "显示第 ".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total)." 条记录,共 <B>$total</B> 条记录";
 if($lastpg<=1) return false; //如果只有一页则跳出

// if($page!=1) $pagecon .=" <a href=&#39;$url=1&#39;>首页</a> "; else $pagecon .=" 首页 ";
// if($prepg) $pagecon .=" <a href=&#39;$url=$prepg&#39;>前页</a> "; else $pagecon .=" 前页 ";
// if($nextpg) $pagecon .=" <a href=&#39;$url=$nextpg&#39;>后页</a> "; else $pagecon .=" 后页 ";
// if($page!=$lastpg) $pagecon.=" <a href=&#39;$url=$lastpg&#39;>尾页</a> "; else $pagecon .=" 尾页 ";

 if($page!=1) $pagecon .=" <a href=&#39;javascript:showpage(\"$url=1\")&#39;>首页</a> "; else $pagecon .=" 首页 ";
 if($prepg) $pagecon .=" <a href= &#39;javascript:showpage(\"$url=$prepg\")&#39;>前页</a> "; else $pagecon .=" 前页 ";
 if($nextpg) $pagecon .=" <a href= &#39;javascript:showpage(\"$url=$nextpg\")&#39;>后页</a> "; else $pagecon .=" 后页 ";
 if($page!=$lastpg) $pagecon.=" <a href= &#39;javascript:showpage(\"$url=$lastpg\")&#39;>尾页</a> "; else $pagecon .=" 尾页 ";

#下拉跳转列表,循环列出所有页码
// $pagecon .=" 到第 <select name=&#39;topage&#39; size=&#39;1&#39; onchange=&#39;window.location=\"$url=\"+this.value&#39;>\n";
 $pagecon .=" 到第 <select name=&#39;topage&#39; size=&#39;1&#39; onchange=&#39;showpage(\"$url=\"+this.value)&#39;>\n";
 for($i=1;$i<=$lastpg;$i++){
  if($i==$page) $pagecon .="<option value=&#39;$i&#39; selected>$i</option>\n";
  else $pagecon .="<option value=&#39;$i&#39;>$i</option>\n";
 }
 $pagecon .="</select> 页,共 $lastpg 页";

 return $page;
 }
//}else die(&#39;pagepide()同名函数已经存在!&#39;);
Copy after login


The following is a simple paging display I made


<?php

 header("Content-type:text/html; charset=utf-8");
 $link = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;123&#39;,&#39;good&#39;);
if (!$link)
{
 die("连接错误: " . mysqli_connect_error());
}

$sql = "SELECT * FROM ecs_category";
$qry = mysqli_query($link,$sql);
$total = mysqli_num_rows($qry);
$per = 10;

$page = pagepide($total,$per);

$sql = "SELECT cat_id,cat_name FROM category limit $sqlfirst,$shownu";
 $result = mysqli_query($link,$sql);

 //$goods = mysqli_fetch_all($result,MYSQLI_ASSOC);

 //mysqli_free_result($result);

echo <<<eof
 <style type="text/css">
 table{width:500px;margin:auto;border: 1px solid black; border-collapse:collapse;text-align:center;}
 td{border: 1px solid black;}
 </style>
 <table>
  <tr><td>序号</td><td>商品编号</td><td>商品类型</td></tr>
eof;

 $num = ($page - 1) * $per;
// foreach ($goods as $k => $v) {
 while($goods = mysqli_fetch_assoc($result)){
 echo "<tr>";
 echo "<td>".++$num."</td>";
 echo "<td>$goods[cat_id]</td>";
 echo "<td>$goods[cat_name]</td>";
 echo "</tr>";
 }

 echo "<tr><td colspan=&#39;3&#39;>$pagecon</td></tr>";
 echo "</table>";
Copy after login


Simple implementation of Ajax non-refresh paging effect

The page will not refresh and jump, the URL will not change, you can see the data interaction of the website

Simple implementation of Ajax non-refresh paging effect

The above is this article All the contents, I hope it will be helpful to everyone's learning, and I also hope that everyone will support the PHP Chinese website.

Related recommendations:

How to implement the Ajax upload image and preview function

A simple method for Ajax asynchronous downloading of files

AJAX checks whether the username is unique

The above is the detailed content of Simple implementation of Ajax non-refresh paging effect. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

6 Ways to Refresh Web Pages on iPhone 6 Ways to Refresh Web Pages on iPhone Feb 05, 2024 pm 02:00 PM

When you browse the web on your iPhone, the loaded content is temporarily stored as long as the browser app remains open. However, the website updates content regularly, so refreshing the page is an effective way to clear out old data and see the latest published content. This way, you always have the latest information and experiences. If you want to refresh the page on iPhone, the following post will explain you all the methods. How to Refresh Web Pages on Safari [4 Methods] There are several methods to refresh the pages you are viewing on the Safari App on iPhone. Method 1: Use the Refresh Button The easiest way to refresh a page you have open on Safari is to use the Refresh option on your browser's tab bar. If Safa

F5 refresh key not working in Windows 11 F5 refresh key not working in Windows 11 Mar 14, 2024 pm 01:01 PM

Is the F5 key not working properly on your Windows 11/10 PC? The F5 key is typically used to refresh the desktop or explorer or reload a web page. However, some of our readers have reported that the F5 key is refreshing their computers and not working properly. How to enable F5 refresh in Windows 11? To refresh your Windows PC, just press the F5 key. On some laptops or desktops, you may need to press the Fn+F5 key combination to complete the refresh operation. Why doesn't F5 refresh work? If pressing the F5 key fails to refresh your computer or you are experiencing issues on Windows 11/10, it may be due to the function keys being locked. Other potential causes include the keyboard or F5 key

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How to quickly refresh a web page? How to quickly refresh a web page? Feb 18, 2024 pm 01:14 PM

Page refresh is very common in our daily network use. When we visit a web page, we sometimes encounter some problems, such as the web page not loading or displaying abnormally, etc. At this time, we usually choose to refresh the page to solve the problem, so how to refresh the page quickly? Let’s discuss the shortcut keys for page refresh. The page refresh shortcut key is a method to quickly refresh the current web page through keyboard operations. In different operating systems and browsers, the shortcut keys for page refresh may be different. Below we use the common W

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

See all articles