我现在遇到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 error: undefined : undefined
所以我不知道是什么问题。
这里是我的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
谢谢!
答案 0 :(得分:2)
您尚未关闭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;
}
答案 1 :(得分:0)
好的,我已经修好了。
在制作@sushanth所说的缺失括号之后,在此之后更改了此$id = $_POST['id']
,这是成功的消息。
我在服务器端完成了这项工作
$response_array['status'] = 'success';
}else {$response_array['status'] = 'error';}
echo json_encode($response_array);
和客户端这个:
success: function(data){
if(data.status == 'success')
// jAlert("Your data is saved succefully");
$('#mydata').html("<span >saved succefully!</span>").delay(3000).fadeOut('fast');
else if(data.status == 'error')
alert("Error on query!");