点击“返回”后阻止重新提交表单按键 [英] Prevent resubmit form after click "back" button

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

问题描述

我有2页:


page1.php:

- 有一个带有文本框和提交的表单按钮。例如:< form name =frm_registeraction =page1.phpmethod =post>


- php和mysql代码将textbox的值存储到数据库中。将值提交到数据库后,Javascript会将页面重定向到 php2.php 。例如:

I have 2 pages :

page1.php :
- has a form with text box and a "submit" button. Eg : <form name="frm_register" action="page1.php" method="post">

- php and mysql code to store the value of textbox to database. Javascript will redirect the page to php2.php after the value is submitted to database. Eg :

$query = "INSERT INTO traceuser (username) VALUES ('{$username}')";
$result = mysql_query($query, $connection);
echo '<script language="javascript">window.location="page2.php";</script>';

page2.php

- mysql检索来自数据库的数据并显示在此页面上。

page2.php
- mysql retrieve the data from database and display on this page.

问题:当我按返回按钮时,浏览器将弹出一条警告信息说该表格将重新提交。单击返回按钮时如何防止重新提交表单?是否需要清除page1.php的缓存?如何使用php或javascript或ajax进行操作?

Problem : When I press "back" button, the browser will pop up a warning message saying that the form will be resubmit. How to prevent resubmit the form when click "back" button? Is it I need to clear the cache of page1.php? How to do it with php or javascript or ajax?



更新1:感谢您更换javascript的答案 window.location =page2.php到php header('Location:home2.php'); 。它解决了80%的问题。剩下的20%问题如下所示:


Update 1 : Thanks for the answer of replacing javascript window.location="page2.php" to php header('Location: home2.php');. It fix 80% of problem. The rest of 20% problem show below :

    if (strtotime($_SESSION['servertime']) < time()-3){ //10800 = 3 hours 3600 = 1 hour
                if (($username != "") AND ($username != $_SESSION[username])){
                    $_SESSION['servertime'] = $servertime; 
                    $_SESSION['username'] = $username;
                    $query = "INSERT INTO traceuser (username) VALUES ('{$username}')";
                    $result = mysql_query($query20, $connection);
                    header('Location: page2.php');
                    exit;
                } else {
                    echo "same name"; //problem here
                }
            }else{
                echo "submit multiple data too fast"; //problem here too.
            }
   }

执行以下步骤时会出现问题:

1)用户成功提交数据,跳转到page2.php查看记录。

2)用户点击返回按钮,跳回page1.php。

3)用户提交数据失败,留在page1.php。 (因为名字太快或相同)

4)用户提交数据成功,跳转到page2.php查看记录。

5)用户点击返回按钮,但浏览器显示警告消息表格将被重新提交。

The problem happen when do the following steps :
1) User submit data successfully, jump to page2.php view records.
2) User click "back" button, jump back to page1.php.
3) User submit data fail, stay on page1.php. (because too fast or same name)
4) User submit data successful, jump to page2.php view records.
5) User click "back" button, but browser shows warning message "form will be resubmited".

问题是由于步骤3.步骤3没有运行标题('Location:page2.php'); ,没有跳转到page2.php。因此它会导致步骤5显示警告消息。如何解决这个问题?



更新2:我已经找到了解决20%问题的解决方案,它完美无缺。我使用 session ['error123'] 来决定是否要显示错误消息同名。如果成功将数据提交到数据库或成功跳转到page2.php,我会终止 session ['error123'] 。我还使用标题('Location:page1.php'); 重定向到自己的页面(同一页面),以使页面忘记之前的表单提交。代码示例:

The problem is because of Step 3. Step 3 didn't run header('Location: page2.php');, didn't jump to page2.php. So it cause Step 5 show the warning message. How to fix this problem?


Update 2 : I have figured out the solution to fix the 20% problem, it works perfectly. I use session['error123'] to decide whether or not want to display the error message "same name". I kill session['error123'] if success submit data to database or if success jump to page2.php. I also use header('Location: page1.php'); to redirect to own page (same page) to make the page forget about form submission previously. Example of codes :

if ($_SESSION['error123'] == "toofast"){
    echo $_SESSION['error123'] ;
}elseif ($_SESSION['error123'] == "samename"){
    echo $_SESSION['error123'] ;
}

if (strtotime($_SESSION['servertime']) < time()-3){ //10800 = 3 hours 3600 = 1 hour
                if (($username != "") AND ($username != $_SESSION['username'])){
                    $_SESSION['username'] = $username;
                    $query = "INSERT INTO traceuser (username) VALUES ('{$username}')";
                    $result = mysql_query($query20, $connection);
                    $_SESSION['error123'] = "aa";
                    header('Location: http://localhost/plekz/page2.php');
                    exit;
                } else {
                    $_SESSION['error123'] = "samename";
                    header('Location: http://localhost/plekz/page1.php');
                    exit;
                }
            }else{
                $_SESSION['error123'] = "toofast";
                header('Location: http://localhost/plekz/page1.php');
                    exit;
            }
        }
    }

注意:你需要缓冲输出<?php ob_start();?> 因为$ _SESSION不能放在header()之前。缓冲区将停止所有输出,包括session,让header()先发送输出。

Note : You need to buffer the output by <?php ob_start();?> because $_SESSION cannot put before header(). Buffer will stop all output including session, let header() send the output first.

推荐答案

而不是

echo'< script language =javascript> window.location =page2.php;< / script>';

你应该在提交后使用header()函数重定向你的用户。

you should use the header() function to redirect your user after the submission.

所以在psuedo中代码,

So in psuedo code,

点击提交页面.php action page1.php
page1.php将数据提交给数据库调用

click submit on page.php action page1.php page1.php submits data to database calls

header('Location: http://example.com/page2.php');

这可以防止你点击回来的问题

This should prevent your clicking back problem

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

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