Home Backend Development PHP Tutorial thinkPHP使用post方式查询时分页失效的解决方法_PHP

thinkPHP使用post方式查询时分页失效的解决方法_PHP

May 28, 2016 pm 01:13 PM
thinkphp Inquire

ThinkPHP

本文实例讲述了thinkPHP使用post方式查询时分页失效的解决方法。分享给大家供大家参考,具体如下:

昨天晚上一直没有解决的php项目中的bug,就在刚才终于搞定,在这里还需要感谢各位大神给的帮助!

具体问题描述

最近遇到一个非常棘手的问题,也是因为刚入手thinkphp。在做项目的过程中,因为需要非常多的查询条件,如果以get方式提交表单的话,会因为url长度限制而报错,所以必须使用post方式提交表单数据,但是在分页的过程中,遇到了问题,因为thinkphp自带的分页是以a标签的形式,进行下一页的,这样查询条件就没有传入后台,导致点击下一页的时候,出现空白。

查看了一下thinkphp官方手册,说了如果是POST方式查询,如何确保分页之后能够保持原先的查询条件呢,具体说明如下图所示:

但是我对手册有一处不解,既然是以通过a标签分页,这样$map(即查询条件参数)中的参数值是怎么传到后台的呢?我在网上也找到了了其他的一些方法,比如用$_REQUEST方式获得参数,说是包括了post和get方式的传值,但是thinkphp的分页是使用a标签来传参的这样的话,参数应该还是无法获得,尝试了几次,结果果然是失败的。还有其他的方法,看了一下都不靠谱。于是在昨晚在博问上提问了一下。有一位大神给我灵感,就是用js修改a标签的href属性。这样确实可以,然后我尝试了一直,终于解决了问题。

解决方法:

<html>
...
...
<body>
<form action="" id="form" method="POST">
....
....
</for>
...
...
<!--分页-->
<div class="Item hr" style="float:right;">
   <div class="current" id="pageBar">{$page}</div>
</div>
...
...
<script type="text/javascript">
  $(function(){
     // 分页(修改链接方法)
    $('#pageBar a').click(function(){ 
      var tmpHref = $(this).attr('href');
      tmpHref = tmpHref.replace(/\/selCon\//,"");
      $("#form").attr("action", tmpHref);
      $("#form").submit();
      return false; 
    });
  }
</script>
</body>
</html>

Copy after login

其中,selCon是form中的参数们,比如name='selCon.a',name='selCon.b'......
按照上面的方法解决了点击下一页的时候,查询参数无法传递的问题。但是我又发现了一个问题,也就是说当我们进入第二页之后,然后再改变查询条件,这时查询出来的结果不是从第一个开始,而是从第二页开始,所以我们这里还需要在查询的时候在js中把p参数(当前页码)设置为1,具体方法如下:

<script type="text/javascript">
  $(function(){
    //查询
    $("#selecting").click(function(){
      $("#form").attr("action", "__URL__/listDept/p/1");
      $("#form").submit();
    });
  }
</script>

Copy after login

希望本文所述对大家基于thinkPHP的php程序设计有所帮助。

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)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages ​​respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

See all articles