【发布时间】:2019-01-23 14:14:04
【问题描述】:
我正在尝试验证是否使用 isset() 发送了正确的表单,但当应用 javascript 延迟时,此验证不是 TRUE。怎么会?检查是否通过POST 方法提交了正确表单的最佳方法是什么?请参阅下面的代码。也许隐藏字段可以解决问题,但我真的很想知道为什么下面的代码没有通过。
<script type="text/javascript">
window.addEventListener('load', function onload(){
var ccform = document.getElementById('cc_form');
if(ccform){
ccform.addEventListener('submit', function before_submit(e){
setTimeout(function wait(){
// After waiting, submit the form.
ccform.submit();
}, 2000);
// Block the form from submitting.
e.preventDefault();
});
}
});
</script>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['cc_form_submit'])) {
//Send the form
//Not working
echo 'ready to send!';
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//Send the form
//Working
echo 'ready to send without ISSET!';
}
?>
<form action="" method="post" class="cc_form" id="cc_form">
<button class="cc_form_submit" type="submit" name="cc_form_submit">Send!</button>
</form>
【问题讨论】:
-
您想在
delay(2000)之后使用jquery 提交您的表单吗?自动 -
没有?点击提交按钮后。目前我在同一页面上有多个表单,我想对正在使用 $_POST 方法处理的正确表单进行额外验证。
-
如果您有多个表单,请使用操作。或ajax
-
或使用隐藏值并签入isset()
-
或者你可以使用 input type="button" 而不是
标签: javascript php forms post isset