我是ajax的菜鸟,但我觉得我的问题很简单。提交表单后,我想让服务器回复给我。这是我当前的代码无法正常工作。我很确定问题出在我的ajax上,因为我知道我的值被放在了dataString中。
HTML
<form method="POST" onSubmit="new_user(this);" >
<input type="submit" value="signed" name="astatus" />
<input type="hidden" value="FaKEIdkEy" name="mark_attend" />
</form>
的Javascript
function new_user(form){
var url = "attendance.php";
var dataString = "status="+form.astatus.value;
dataString = dataString+"&id="+form.mark_attend.value;
$.ajax({
type: "POST",
url: url,
data: dataString,
success: function(data) {
alert(data);
}
});
}
php (attendance.php)
if(!empty($_POST))
{
echo "response";
}
关于如何修复的任何想法?
答案 0 :(得分:3)
您应该在函数末尾添加return false;
以阻止表单提交。
function new_user(form) {
var url = "attendance.php";
var dataString = "status="+form.astatus.value+"&id="+form.mark_attend.value;
$.ajax({
type: "POST",
url: url,
data: dataString,
success: function(data) {
alert(data);
}
});
return false;
}
答案 1 :(得分:0)
$.ajax({
type: "POST",
url: url,
data: dataString,
success: function(data) {
console.log(data);
}
});
控制台是否显示“响应”?
答案 2 :(得分:0)
You are calling ajax at on submit , so when ajax run after that page is reload so data is vanished.
<form method="POST" >
<input type="button" value="signed" name="astatus" onclick="new_user()" />
<input type="hidden" value="FaKEIdkEy" name="mark_attend" id="mark_attend"/>
</form>
and get the value with jquery
var fakeidkey=$_('#mark_attend').val();
and then concat it with your varaible .