简体   繁体   English

使用php分页时避免丢失POST数据

[英]Avoid losing POST data when using php pagination

I am working on a renting website that transfer date from page to another, for example the user enter a date and some information and when he goes to another page he should find the information that he entered in the first page. 我正在一个出租网站上工作,该网站将日期从页面转移到另一个页面,例如,用户输入日期和一些信息,当他进入另一个页面时,他应该找到他在第一页中输入的信息。 Everything works fine except that when I add pagination like this: [1] [2] [3] [4] every time I click on a page number in the pagination the POST data will be lost. 一切正常,除了当我这样添加分页时:[1] [2] [3] [4]每次我在分页中单击页码时,POST数据都会丢失。 For example if I clicked number 2 in the pagination I get this message: 例如,如果我在分页中单击数字2,则会收到以下消息:

Undefined index: date......

I found some solutions which is using get method but I don't know how to use get method in this situation. 我找到了一些使用get方法的解决方案,但在这种情况下我不知道如何使用get方法。

What should I change to my code to avoid losing POST data? 为了避免丢失POST数据,我应该对代码进行哪些更改?

here is my full code: 这是我的完整代码:

 <?php
$date= $_POST['date']; // the post data from previous page
$info= $_POST['info']; // the post data from previous page

?>  


<form action="next_page.php" method="post" name="myForm"> 


<input name="date" type="hidden" value="<?php echo $date; ?>" />
<input name="info" type="hidden" value="<?php echo $info; ?>" />




<?php

include(db.php);


$q="select count(*) \"total\"  from users";
$ros1=mysql_query($q,$link);
$row=(mysql_fetch_array($ros1));
$total=$row['total'];
$dis=8;
$total_page=ceil($total/$dis);


$page_cur=(isset($_GET['page']))?$_GET['page']:1;
$k=($page_cur-1)*$dis;

$query="SELECT * FROM cars limit $k,$dis";
$ros=mysql_query($query,$link);

while($row = mysql_fetch_array($ros))
{

 echo  $row["user_id"];

echo  $row["user_name"];

echo  $row["user_email"];
}
for($i=1;$i<=$total_page;$i++){
if($page_cur==$i){
echo ' <input type="button" value="'.$i.'"> ';
}
else{
echo '<a href="carstype.php?page='.$i.'"> <input type="button" value="'.$i.'"> </a>';
}}
?>
<input  type="submit" name="userId" value="' .$row['user_id'] . '" /> 

</form>  

You should store this information somewhere between your http-requests. 您应该将这些信息存储在您的http请求之间的某个位置。 There are some possible solutions for that: 有一些可能的解决方案:

  1. you can store this information in the database 您可以将这些信息存储在数据库中
  2. in session (temporarily store, if you don't need this data in your database) 会话中(临时存储,如果您不需要数据库中的数据)
  3. store in every html page as a hidden field 存储在每个html页面中作为隐藏字段

You can use PHP sessions to store users input on the server: 您可以使用PHP会话将用户输入存储在服务器上:

PHP Sessions PHP会话

Keep all the data in $_SESSION and read it from there when it's needed in the page. 将所有数据保存在$_SESSION并在页面中需要时从那里读取。 Update the stored values when the users inputs something new. 用户输入新内容时,更新存储的值。

You should use GET method instead of POST method. 您应该使用GET方法而不是POST方法。

    <?php
$date = $_GET['date']; // the post data from previous page
$info = $_GET['info']; // the post data from previous page
?>  


<form action="next_page.php" method="get" name="myForm"> 


    <input name="date" type="hidden" value="<?php echo $date; ?>" />
    <input name="info" type="hidden" value="<?php echo $info; ?>" />




    <?php
    include(db . php);


    $q = "select count(*) \"total\"  from users";
    $ros1 = mysql_query($q, $link);
    $row = (mysql_fetch_array($ros1));
    $total = $row['total'];
    $dis = 8;
    $total_page = ceil($total / $dis);


    $page_cur = (isset($_GET['page'])) ? $_GET['page'] : 1;
    $k = ($page_cur - 1) * $dis;

    $query = "SELECT * FROM cars limit $k,$dis";
    $ros = mysql_query($query, $link);

    while ($row = mysql_fetch_array($ros)) {

        echo $row["user_id"];

        echo $row["user_name"];

        echo $row["user_email"];
    }
    for ($i = 1; $i <= $total_page; $i++) {
        if ($page_cur == $i) {
            echo ' <input type="button" value="' . $i . '"> ';
        } else {
            echo '<a href="carstype.php?page=' . $i . '&date=' . $date . '&info=' . $info . '"> $i </a>';
        }
    }
    ?>
    <input  type="submit" name="userId" value="' .$row['user_id'] . '" /> 

</form>

A good way in my opinion is to start the seach with a POST form and if the results exceed a limit then construct pagination links which really are post forms incorporating the search keyword and target page in the form of hidden variables. 我认为一个好方法是以POST形式开始搜索,如果结果超出限制,则构建分页链接,这些链接实际上是包含隐藏关键字形式的search关键字和目标页面的帖子形式。

Passing the search keyword as a get parameter needs care (url encode) because urls have limits on the characters they allow (ascii letters and digits, and characters such as /, ~, -, _ etc ) for example a user might search for something in japanese or greek or use symbols. 将搜索关键字作为get参数传递时需要小心(URL编码),因为URL对它们允许的字符(ASCII字母和数字,以及诸如/,〜,-,_等的字符)有限制,例如,用户可能会搜索某些内容用日语或希腊语或使用符号。

Passing the search keyword as a session variable also needs care. 将search关键字作为会话变量传递也需要注意。 Consider this scenario: a user has opened multiple result pages, but all pages will hereafter have access to the last processed session saved keyword. 考虑这种情况:用户打开了多个结果页面,但是此后所有页面都可以访问上次处理的会话保存的关键字。 Also storing the search keyword in a session requires coordination by the developer such as when to destroy the session variable etc. 同样在会话中存储搜索关键字需要开发人员进行协调,例如何时销毁会话变量等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM