我有注册表格。我看到一切都很好,并在控制台中正确显示,但是当成功插入记录以及显示成功消息时,我无法重定向页面。它也没有显示任何错误,如邮件已存在于数据库中。这是我的表格
<script>
$(document).ready(function () {
$("#registFrm").submit(function () {
var data = $("#registFrm").serialize();
signupRecords(data);
return false;
});
function signupRecords(data) {
$.ajax({
url: 'signupProcess.php',
data: data,
type: 'POST',
dataType: 'json',
success: function (data) {
if(data.code =="200" ) {
alert('success : You have successfully signUp now please login');
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 5000);
} else {
$(".display-error").html("<ul>"+data.msg+"</ul>");
$(".display-error").css("display","block");
}
},
error: function (jqXHR, exception) {
console.log(jqXHR);
// Your error handling logic here..
}
});
}
});
</script>
这是我的signupProcess.php执行
if (empty($errorMSG)) {
$qry = $db->prepare('SELECT email FROM users WHERE email=?');
$qry->bindParam(1, $remail);
$qry->execute();
if ($qry->rowCount() > 0 ) {
echo json_encode(['code' => 400, 'msg' => 'Email Already Exist']);
exit;
} else {
$stmt = $db->prepare("INSERT INTO users(name,email,password,mobile) VALUES(:name,:email,:password,:mobile)");
$stmt->execute(array(':name' => $rname, ':email' => $remail, ':password' => $rpassword, ':mobile' => $rmobile));
$affected_rows = $stmt->rowCount();
if ($affected_rows == 1) {
// starts the session created if login info is correct
echo json_encode(['code' => 200, 'msg' => 'Successfully singUp']);
exit;
} else {
echo json_encode(['code' => 400]);
exit;
}
} }else {
echo json_encode(['code' => 404, 'msg' => $errorMSG]);
}
答案 0 :(得分:1)
请按照我的步骤
<script>
$(document).ready(function () {
$("#registFrm").submit(function () {
var data = $("#registFrm").serialize();
signupRecords(data);
return false;
});
function signupRecords(data) {
$.ajax({
url: 'signupProcess.php',
data: data,
type: 'POST',
dataType: 'json',
success: function (data) {
if(data.code ==200 ) {
alert('success : You have successfully signUp now please login');
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 5000);
} else {
$(".display-error").html("<ul>"+data.msg+"</ul>");
$(".display-error").css("display","block");
}
},
error: function (jqXHR, exception) {
console.log(jqXHR);
// Your error handling logic here..
}
});
}
});
</script>