我正在使用Bootstrap签名表单,我正在使用此代码:
$.ajax({
type: "POST",
url: 'process/register.php',
data: $(this).serialize(),
dataType: "html",
success: function(msg){
alert('23');
},
error: function(error) {
console.log("an error", error);
}
});
然而没有任何反应。表单有效,数据库有新增功能,但回调永远不会运行。
根据要求,这是register.php
代码:
$email = $_POST['Email_address'];
$pword = $_POST['Password'];
$pword = md5($pword);
$result = $db->query("INSERT INTO users (`name`,`password`) VALUES ('" . $email . "','" . $pword . "')");
if($result != FALSE){
echo "true";
} else {
echo "false";
}
echo "!!!!!!!!!!";
如果有帮助,这里是HTML表单代码:
<form class="form-signin" method="post" enctype="multipart/form-data" action="" id="regForm">
<h2 class="form-signin-heading" style="width:500px;">Register</h2>
<input type="text" class="form-control" placeholder="Email address" name="Email_address" id="Email_address" autofocus required>
<input type="password" class="form-control" placeholder="Password" name="Password" id="Password" required><br>
<button class="btn btn-lg btn-primary btn-block" type="submit" id="regSubmit">Register</button>
</form>
答案 0 :(得分:0)
使用json! :)
$.ajax({
type: "POST",
url: 'process/register.php',
data: $(this).serialize(),
dataType: "json",
error: function(error) {
console.log("an error", error);
}
}).done(function(json){
// your code here
});
服务器端:
$email = $_POST['Email_address'];
$pword = $_POST['Password'];
$pword = md5($pword);
$result = $db->query("INSERT INTO users (`name`,`password`) VALUES ('" . $email . "','" . $pword . "')");
$output = new stdClass();
if($result != false){
$output->status="success";
} else {
$output->status="fail";
$output->message="Wrong password";
}
print json_encode($output);
答案 1 :(得分:0)
将JavaScript更改为:
$.ajax({
type: "POST",
url: 'process/register.php',
data: $(this).serialize(),
dataType: "html",
async:false,
success: function(msg){
alert('23');
},
error: function(error) {
console.log("an error", error);
}
});
解决了它。 (添加行async:false
)。但是,它仍然更新。事实证明真正的问题是页面正在刷新(使用async:true
,我认为页面在回调运行之前刷新,更改它使回调首先运行)。因此,这解决了它,并且不需要async:false
:
$("#regForm").submit(function () {
$.ajax({
type: "POST",
url: 'process/register.php',
data: $(this).serialize(),
dataType: "html",
success: function(msg){
alert('23');
},
error: function(error) {
console.log("an error", error);
}
});
return false;
});
(在提交功能的末尾添加return false
。)
答案 2 :(得分:-1)
我想你的php中没有echo 'any string';
吗?
function(msg)
需要回报,甚至只需回显1表示成功(添加到db)或回显0(未能插入)
所以你可以在你的成功处理程序中做(注意:ajax连接到php并且有一个返回值)= =
success: function(msg) {
if(msg == 'success') {
alert('yay');
}
else {
alert('fail');
}
}