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

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

问题描述

我认为这个问题经常发生在 web 应用程序开发中.但我会尽量详细解释我的问题.

I think that this problem occurs often on a web application development. But I'll try to explain in details my problem.

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

I'd like to know how to correct this behavior, for example, when I have a block of 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 the form gets submitted, the data get inserted into the database, and the message Operation Done is produced. Then, if I refreshed the page, the data would get inserted into the database again.

如何避免这个问题?任何建议将不胜感激:)

How this problem can be avoided? Any suggestion will be appreciated :)

推荐答案

在创建操作后不显示响应;而是在操作完成后重定向到另一个页面.如果有人刷新,他们正在刷新您重定向到的 GET 请求页面.

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天全站免登陆