是否可以保持表单提交以显示通知几秒钟?
在提交回调表单后重新加载页面之前,我想显示以下内容:“感谢您的回调请求。我们会尽快与您联系。”
如果您使用 AJAX 提交,则无需刷新。
以此为例:
<form>
<input type="text" name="fname"/>
<input type="text" name="lname"/>
<input type="text" name="email"/>
<input type="text" name="address"/>
<input type="password" name="password"/>
<!--
Here you have two options. Use <a></a> or an input with type="submit"
since you're using AJAX, I reccomend using <a href="#" id="submit_form">Submit</a>
-->
<a href="#" id="submit_form">Submit</a>
</form>
感谢您的回电请求。我们会尽快与您联系。
然后在javascript上:
<script>
$(document).ready(function(){
$("#submit_form").bind("click",function(){
//do a $.post();
$.post("submit/this.php",something : "content_of_something",
function(response){
if(response==1)//or whatever you want
$('#some_id').fadeIn(function() {
setTimeout(function(){
window.location.reload();
},3000);
});
else
alert("Error ocurred");
}
);
});
});
</script>
在 PHP 上,检查变量是否通过 $_POST 到达服务器(出于调试目的,在服务器上执行 var_export($_POST) 并在客户端上在函数(响应)之后放置一个警报(响应))。如果一切按预期进行,则回显 1(因此响应将匹配 1== 响应 == 1),否则,您可以回显您想要的其他内容。
您可以使用 preventDefault() 来阻止表单提交,显示您想要的信息并在所需的延迟后在 setTimout() 中 submit() 表单。