我相信这应该做你想做的事,它至少应该给你一个想法 - 它最初是关于防止实际重复提交的更大帖子 Stop multiple submits for a html from 的一部分。
再看一遍,希望你能对自己提出太多要求,所以这里有一个被替换的版本。您可能想要重新实现提交按钮的禁用,但您似乎确实希望它连续启用,因此为了清楚起见,我在此处删除了该代码。
您可能需要调整计数 - 不确定可能会发生多少次自动提交 - 就目前而言,这将占一个,但在那之后它不会接受任何东西,直到下一次“真正的”提交。
此时,PHP 代码可以请求用户干预,如“您真的提交了两次吗?”或者它可以简单地忽略提交。 JavaScript 也是如此,根据@symcbean 的建议,它可以在重新提交之前要求确认 - 皮带和大括号总是一个好主意!如果您在提交时单击,您应该可以选择故意提交两次,或者取消。
据我所知,在这个版本中,如果您注释掉 keyPress 事件 submit_this(); 调用 F5 按键,它将不接受 F5 提交以进行刷新。 - 当 keyPress 功能运行时,按 F5 提交 不会出现刷新时通常会收到的确认警告。
我已经对此进行了测试,但只能等待您的确认,看看它是否也能处理自动重新提交。如果单击浏览器刷新按钮,则 PHP 不接受提交。由Ctrl+R 刷新产生的提交也不被接受。希望这反映了重新提交的情况。由于无法判断确认/取消是否有效,我已将警报恢复为比提交页面更快的点击。你可以尝试一下!
<?php
session_start();
if (!isset($_POST['hidden_input'])) $hidden_input = 1;
if (!isset($_SESSION['submit_count_input'])) $_SESSION['submit_count_input'] = -1;
if (isset($_POST['hidden_input'])) $hidden_input = intval($_POST['hidden_input']);
// detect difference between JavaScript submitted value and possible automatic ones
if (($hidden_input - $_SESSION['submit_count_input']) == 1){
// do your processing here
//$submit_count_input should always be 1 behind
echo "Thank you. Form processed";
}else{
// correct the balance between $hidden_input and $submit_count_input
$_SESSION['submit_count_input'] = ($hidden_input - 1); // will be incremented so will be -1 effectively
echo "Form not processed.";
}
// remove this echo section - just for testing
echo " Control Token: " . $hidden_input . " ";
echo " Submit Count: " . $_SESSION['submit_count_input']; // to show it submits but does not process form
$_SESSION['submit_count_input']++;
?>
<!DOCTYPE html>
<html>
<head>
<script language="javascript">
document.onkeydown=function(e) {
var event = window.event || e;
if (event.keyCode == 116) {
event.keyCode = 0;
submit_this();
return false;
}
}
function submit_this(){
if(document.getElementById('submit_count_input').value - document.getElementById('hidden_input').value == -1) {
// Warning when submit clicked before previous submission finished confirm/cancel option as per link below could be used
alert("Easy Tiger - please slow down a bit!");
}else{
document.getElementById('hidden_input').value = 1+parseInt(document.getElementById('hidden_input').value);
document.form.submit();
}
}
</script>
</head>
<body>
<form name="form" id="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post">
<input type="hidden" name="hidden_input" id="hidden_input" value="<?php echo $hidden_input ?>" />
<input type="hidden" name="submit_count_input" id="submit_count_input" value="<?php echo intval($_SESSION['submit_count_input']) ?>" />
<input type="button" name="sbutton" id="sbutton" value="Submit" onclick="submit_this();" />
</form>
<a href="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>">Click to start a new submission</a>
</body>
</html>
确认/取消 JavaScript 对话框JavaScript confirm cancel button not stopping JavaScript
关于保留提交表单中的值的次要问题,没有会话,那么即使对于很多表单,如果您在每个表单文本框元素的值中使用value="<?php echo htmlspecialchars($_POST['form_element_name_value'] ?>",这应该是相当简单的。确保每个文本框都有一个 name="something" 供 php 提取为 $_POST - 抱歉,如果这太明显了。
为了满足您的用户能够使用 F5 提交页面的愿望,您可以捕获功能并将其颠覆为实际提交,但这可能是一个令人讨厌的想法,尽管它确实有效......而且它会保持 PHP 端提供已提交的数据,这些数据仍可能与失败连接的自动重新提交区分开来。
document.onkeydown=function(e) {
var event = window.event || e;
if (event.keyCode == 116) {
event.keyCode = 0;
submit_this();
return false; }
}
要捕获浏览器上的实际刷新按钮,您可以查看How to know whether refresh button or browser back button is clicked in firefox,但颠覆浏览器行为以强制提供给定的用户体验通常是不受欢迎的。