由于刷新页面而避免提交的最佳方法 [英] Best way to avoid the submit due to a refresh of the page

查看:96
本文介绍了由于刷新页面而避免提交的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题经常发生在一个web应用程序上。但我会详细解释我的烦恼。

I think that this problem happens often on a web application. But I'll explain in details my troubles.

我想知道如何摆脱这种行为,例如当我有这样的代码时:

I'd like to know how to get rid of this behaviour, for example when I've a code like this :

<?
    if (isset($_POST['name'])) {
        ... operation on database, like to insert $_POST['name'] in a table ...
        echo "Operation Done";
        die();
    }

?>

<form action='page.php' method='post' name="myForm">
    <input type="text" maxlength="50" name="name" class="input400" />
    <input type="submit" name="Submit" />
</form>

当我提交表单时,我在数据库中插入数据,并且收到消息完成操作。相比之下,如果我刷新页面,数据将再次被删除。

When I submit the form, I insert the data on database, and I get the message Operation Done. Than, if I refresh the page, the data will insered again.

如何避免这种情况?任何建议都会很好:)

How can I avoid from this situation? Any suggestions will be good :)

推荐答案

创建操作后不要显示响应;而是在动作完成后重定向到另一个页面。

Don't show the response after your create action; redirect to another page after the action completes instead. If someone refreshes, they're refreshing the GET requested page you redirected to.

// submit
// set success flash message (you are using a framework, right?)
header('Location: /path/to/record');
exit;

这篇关于由于刷新页面而避免提交的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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