我现在遇到了 ajax 发送表单的问题,找不到错误在哪里,
这里是我的 html:
<form method="post" id="update-profile" name="update-form" action="process.php">
<input name="username" type="text" id="username" value="<?php echo $username;?>"/>
........
<input name="update_profile" type="button" class="submit" id="update_profile" value="Update" onclick="return formValidation();" />
这是我的 javascript :
function formValidation() {
// other validations here
if (validsex(umsex, ufsex)) {
$('#mydata').html('Saving ...<img src="../images/loading.gif" />');
$.ajax({
url: "process.php",
dataType: 'json' ,
data :{ id : document.getElementById('iidd').value,
username : document.getElementById('username').value,
name : document.getElementById('name').value,
password : document.getElementById('password').value,
slist: document.getElementById('slist').value,
sexs : sexs,
update_profile : "update_profile",
type : 'POST',
success: function(msg){
$('#mydata').html("<span >saved succefully </span>").delay(3000).fadeOut('fast');
}
}
});
}
}
}
}
}
return false;
}
甚至调试过这个
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
},
它说:AJAX 错误:未定义:未定义
所以我不知道是什么问题。
这里是我的 process.php :
if (isset($_POST['update_profile'])) {
$id = $_POST['iidd'];
$username = mysql_real_escape_string($_POST['username']);
$name = mysql_real_escape_string($_POST['name']);
$password = mysql_real_escape_string($_POST['password']);
$country = mysql_real_escape_string($_POST['slist']);
$sex = mysql_real_escape_string($_POST['sexs']);
//update query here
谢谢!
最佳答案
您还没有关闭data
属性的大括号
data: {
id: document.getElementById('iidd').value,
username: document.getElementById('username').value,
name: document.getElementById('name').value,
password: document.getElementById('password').value,
slist: document.getElementById('slist').value,
sexs: sexs,
update_profile: "update_profile"
}, <---- Missing this
而且我不明白您的代码中所有其他额外大括号的原因。
应该看起来像
function formValidation() {
// other validations here
if (validsex(umsex, ufsex)) {
$('#mydata').html('Saving ...<img src="../images/loading.gif" />');
$.ajax({
url: "process.php",
dataType: 'json',
data: {
id: document.getElementById('iidd').value,
username: document.getElementById('username').value,
name: document.getElementById('name').value,
password: document.getElementById('password').value,
slist: document.getElementById('slist').value,
sexs: sexs,
update_profile: "update_profile"
},
type: 'POST',
success: function (msg) {
$('#mydata')
.html("<span >saved succefully </span>")
.delay(3000).fadeOut('fast');
}
});
}
return false;
}
关于php - 为什么我的ajax没有提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246272/