防止在点击返回按钮时重新提交表单

2022-01-04 00:00:00 refresh forms get php back

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

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...
}
?>

现在如前所述,如果我点击链接,它会带我到view.php?id=whatever".在该页面上,我只是从 url 中获取 ID 以显示该单个条目:

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
}

?>

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

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");

相关文章