我正在尝试使用jQuery post处理表单数据,如下所示:
<script type="text/javascript">
$('#settingsForm').submit( function(){ $('input[type=submit]', this).attr('disabled', 'disabled ');});
function scheck(){
var oldanswer = $("#oldanswer");
var newanswer = $("#newanswer");
var url = "settingsupdate.php";
//This part of the code works fine. Errors are displayed normally
if(oldanswer.val() == ""){
$('#output').html('Oops you left a field empty. Please fill it').show().fadeOut(6000);
}
else if (newanswer.val() == ""){
$('#output').html('Oops you left a field empty. Please fill it').show().fadeOut(6000);
}
else{
$.post(url,{oldans: oldanswer.val(), newans: newanswer.val()},function (data){
$("#security_check").slideup("fast");//this is a div containing the form
$("#output").html(data).show().fadeOut(10000);//this div outputs the returned data
document.settingsForm.oldanswer.value = "";
document.settingsForm.newanswer.value = "";
});
}
}
</script>
在上面的代码中,错误检查效果很好。如果字段为空,页面将显示相应的错误消息。但是,如果两个字段都不为空并且提交了表单,则页面将保持静态。没有任何东西从php脚本回显到页面。
settingsupdate.php
if(isset($_POST['newans'])){
$oldans = htmlspecialchars($_POST['oldans']);
$newans = htmlspecialchars($_POST['newans']);
echo $newans;
}
表格
<form action="javascript:scheck();"id="settingsForm" name="settingsForm" method="post">
<input type="text" id="oldanswer" name="oldanswer"/>
<input type="text" id="newanswer" name="newanswer"/>
<input type="submit" id="submitset" name="submitset"/>
</form>
我检查了print_r($ POST)并返回一个空数组。我不知道为什么。任何人都可以帮我让这段代码正常工作。感谢