点击返回按钮时防止表单重新提交 [英] Prevent Form resubmission upon hitting back button

查看:158
本文介绍了点击返回按钮时防止表单重新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里和其他地方搜索了很多帖子,但似乎无法找到解决我的问题的方法。
我有一个显示数据库条目的页面:database.php。这些条目可以用表单过滤。当我过滤它们,只显示我感兴趣的,我可以点击一个条目(作为一个链接),带我到该条目页面(通过PHP GET)。当我在该条目页面(即view.php?id = 1)并点击后退按钮(返回到database.php)时,过滤器表单需要确认表单重新提交。有什么办法可以防止这种情况发生?



这里有一些(简化的)代码示例:

Database.php:

 < form> 
< select>
< option> 1< / option>
< option> 2
< option>
< / select>
< input type =submitname =apply_filter/>
< / form>
<?php
if(isset($ _POST [apply_filter])){//显示已过滤条目
$ filter = $ _POST [filter];
$ q =Select * from table where col ='。 $过滤器。 ;
$ r = mysql_query($ q);
} else {//显示所有条目
$ q =Select * from table;
$ r = mysql_query($ q);
}
while($ rec = mysql_fetch_assoc($ r)){
echo< a href ='view.php?id =。 $ rec [id]。 >中。 $ rec [name]。 < / a>< br />; //这是view.php页面的链接是...
}
?>

现在如上所述,如果我点击链接,它会带我到view.php?id =什么。在该页面上,我只需从网址中获取ID即可显示该单个条目:
$ b view.php:

 <?php 
$ id = $ _GET [id];
$ q =Select * from table where id ='。 $ id。 ;
$ r = mysql_query($ q);
while(){
//显示条目
}

?>

如果我现在点击后退按钮,database.php上的表单(用于过滤数据库结果)需要确认重新提交。这不仅非常烦人,它对我也没用。



我该如何解决这个问题?我希望代码示例和我的问题的解释是足够的。如果不让我知道,我会尝试指定。

解决方案

我知道这个问题很旧,但自己有这个问题,我发现的两行代码是:

  header(Cache-Control:no cache); 
session_cache_limiter(private_no_expire);


I have searched many posts here and elsewhere but can't seem to find a solution to my problem. I have a page which displays database entries: database.php. These entries can be filtered with a form. When I filter them and only display the ones I am interested in I can click an entry (as a link) which takes me to that entries page (via php GET). When I am on that entries page (i.e., "view.php?id=1") and hit the back button (back to database.php), the filter form requires to confirm the form resubmission. Is there any way to prevent this?

here are some (simplified) code examples:

Database.php:

<form>
    <select>
        <option>1</option>
        <option>2
        <option>
    </select>
    <input type="submit" name="apply_filter" />
</form>
<?php
if ( isset( $_POST[ "apply_filter" ] ) ) { // display filtered entries
    $filter = $_POST[ "filter" ];
    $q = "Select * from table where col = '" . $filter . "'";
    $r = mysql_query( $q );
} else { // display all entries
    $q = "Select * from table";
    $r = mysql_query( $q );
}
while ( $rec = mysql_fetch_assoc( $r ) ) {
    echo "<a href='view.php?id=" . $rec[ "id" ] . "'>" . $rec[ "name" ] . "</a><br />"; // this is where the link to the view.php page is...
}
?>

Now as mentioned, if I click on the link, it takes me to "view.php?id=whatever". On that page, I just get the ID from the url to display that single entry:

view.php:

<?php
$id = $_GET[ "id" ];
$q = "Select * from table where id = '" . $id . "'";
$r = mysql_query( $q );
while (  ) {
    // display entry
}

?>

If I now hit the back button, the form on database.php (the one used to filter the DB results) requires confirmation for resubmission. Not only is this very annoying, its also useless to me.

How can I fix this? I hope the code examples and explanation of my problem are sufficient. If not let me know and I'll try to specify.

解决方案

I know this question is old, but having this issue myself, two lines I've discovered that works are:

header("Cache-Control: no cache");
session_cache_limiter("private_no_expire");

这篇关于点击返回按钮时防止表单重新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆