如何在提交表单后清除信息,以便在页面刷新后不显示此错误?
请参见以下Chrome图片:
该对话框的文本为:
您正在查找的页面使用了您输入的信息。
返回该页面可能导致您采取的任何操作被重复执行。
您是否要继续?
我希望这个对话框不再出现。
如何在提交表单后清除信息,以便在页面刷新后不显示此错误?
请参见以下Chrome图片:
该对话框的文本为:
您正在查找的页面使用了您输入的信息。
返回该页面可能导致您采取的任何操作被重复执行。
您是否要继续?
我希望这个对话框不再出现。
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
编辑:距离我最初发布这个答案已经过去了几年,虽然我得到了一些赞,但我并不满意以前的答案,所以我完全重做了它。希望这可以帮到你。
GET
和POST
:摆脱此错误消息的一种方法是使您的表单使用GET
而不是POST
。但请记住,这并不总是一个适当的解决方案(请阅读下文)。
如果正在执行不希望重复的操作、传输敏感信息或者表单中包含文件上传或所有数据的长度超过2000个字符,则始终使用POST。
需要使用POST
的示例包括:
<input type="file">
字段的GET
,则仅发送文件名到服务器,这在99.73%的情况下不是您想要的。)POST
执行破坏性操作的本质,Web 设计师应该防止用户意外(或故意)刷新页面而再次执行它们。许多用户甚至不知道这个对话框的含义,因此只会单击“继续”。如果这是在“提交付款”请求之后呢?付款会再次发送吗?if(!empty($_POST['username'] && !empty($_POST['password'])) {
$user = new User;
$user->login($_POST['username'], $_POST['password']);
if ($user->isLoggedIn()) {
header("Location: /admin/welcome.php");
exit;
}
else {
header("Location: /login.php?invalid_login");
}
}
if (isset($_GET['invalid_login'])) {
echo "Your username and password combination is invalid";
}
这与表单本身或其中的值无关。它是由浏览器触发的,以防止用户使用缓存数据重复发送相同的请求。如果您确实需要启用结果页面的刷新,您应该通过PHP (header('Location:result.php');
)或其他服务器端语言来重定向用户。Meta标签解决方案也可以在刷新时禁用重新发送。
redirect('somecontroller/somefunction');
作为简写。有关成功消息,请阅读用户指南中的flash会话部分。 - toopayhttp://test.com/test.php
上。header('Location: http://test.com/test.php');
看起来你正在寻找Post/Redirect/Get
模式。
作为另一种解决方案,你可以完全停止使用重定向。
你可以立即处理和呈现处理结果,而无需POST
确认警报。你只需要操作浏览器历史记录对象:
history.replaceState("", "", "/the/result/page")
$.ajax({
type: 'POST',
url: 'ajax/comment-on-video.php',
data: {
comment: $('#idOfInputField').val();
},
success: function(obj) {
if(obj === 'true') {
//Some code that recreates the inserted comment on the page.
}
}
});
comment-on-video.php
,并创建类似以下内容的内容:<?php
session_start();
if(isset($_POST['comment'])) {
$comment = mysqli_real_escape_string($db, $_POST['comment']);
//Given you are logged in and store the user id in the session.
$user = $_SESSION['user_id'];
$query = "INSERT INTO `comments` (`comment_text`, `user_id`) VALUES ($comment, $user);";
$result = mysqli_query($db, $query);
if($result) {
echo true;
exit();
}
}
echo false;
exit();
?>
我曾遇到一种情况,无法使用以上任何答案。我的情况涉及与搜索页面的工作,如果用户在导航到任何搜索结果后单击返回,则会得到“确认表单重新提交”。我编写了以下javascript来解决此问题。它不是一个很好的解决方法,因为它有点闪烁,并且不适用于IE8或更早版本。尽管如此,这可能对处理此问题的人有用或有趣。
jQuery(document).ready(function () {
//feature test
if (!history)
return;
var searchBox = jQuery("#searchfield");
//This occurs when the user get here using the back button
if (history.state && history.state.searchTerm != null && history.state.searchTerm != "" && history.state.loaded != null && history.state.loaded == 0) {
searchBox.val(history.state.searchTerm);
//don't chain reloads
history.replaceState({ searchTerm: history.state.searchTerm, page: history.state.page, loaded: 1 }, "", document.URL);
//perform POST
document.getElementById("myForm").submit();
return;
}
//This occurs the first time the user hits this page.
history.replaceState({ searchTerm: searchBox.val(), page: pageNumber, loaded: 0 }, "", document.URL);
});
window.location.replace
,它会打开该页面并删除被重定向页面的历史记录条目。 - Justin Liu我找到了一种非正统的方法来完成这个。
只需将脚本页面放入一个iframe中。这样做可以使页面刷新,即使在旧版浏览器上也不会出现“确认表单重新提交”的消息。
使用不同的方法来加载表单并保存/处理表单。
例如。
Login.php
在 Login/index
加载登录表单
在 Login/validate
进行登录验证
将用户重定向到 User/dashboard
将用户重定向到 login/index