按下F5时避免在php中重新提交表单 [英] Avoiding form resubmit in php when pressing f5

查看:146
本文介绍了按下F5时避免在php中重新提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有以下代码(使用php和smarty),试图避免在按f5键时重新提交表单:

I have the following code on my site (using php and smarty) to try and avoid a form resubmitting when I hit f5:

if ($this->bln_added == false) {
    if (isset($_POST['submit'])) {
        $this->obj_site->obj_smarty->assign('title', $_POST['tas_heading']);
        $this->obj_site->obj_smarty->assign('desc', $_POST['tas_description']);
    }
} else {
    $this->obj_site->obj_smarty->assign('title', '');
    $this->obj_site->obj_smarty->assign('desc', '');
    unset($_POST);
}

bln_added默认情况下为false,但一旦成功提交表单,则更改为true.模板中使用了smarty变量title和desc来将表单内容保留在其中,以防出现用户错误并且需要更改其输入内容.

bln_added is false by default, but changes to true once the form is successfully submitted. The smarty variables title and desc are used in the template to keep the form content there in case there is a user error and they need to change what they entered.

如果成功提交了表单,则它将bln_added = true设置为第二位,因此代码的第二位不仅应清除表单字段,还应清除$ _POST空.但是,如果我按f5键,则发布数据仍然存在.

If the form is submitted successfully it sets bln_added = true, so the second bit of code should not only clear the form fields, but also empty $_POST. But if I press f5 the post data is still there.

有什么想法吗?

推荐答案

您的方法在理论上可以起作用,但是有一种更简单的方法.

Your method could work in theory, but there's a much easier way.

成功提交表单后,执行重定向.到哪里都没关系,但是会清除$ _POST.

After submitting the form successfully, perform a redirect. It doesn't matter where to, but it'll clear the $_POST.

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

对于您来说,听起来好像您想重定向到您已经在的页面.如果要显示确认消息,请在URL上附加$ _GET参数.

In your case, it sounds like you want to redirect to the page you're already on. Append a $_GET parameter to the URL if you want to display a confirmation message.

希望这会有所帮助,

汤姆

这篇关于按下F5时避免在php中重新提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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