[英]Ajax PHP - Check returned response/data value from php
基本上,我正在验证一个注册表单,并使用ajax调用php脚本来检查是否已经存在一个电子邮件地址或执行查询。 如果一个电子邮件地址已经存在(通过计数查询进行检查),我正在将错误回显到ajax函数(通过响应)-但是不会被读取。
我曾尝试研究类似的主题,但没有运气。
阿贾克斯
$.ajax({
type: 'POST',
url: 'signup-user.php',
dataType: 'text',
data: $('form').serialize(),
success: function(response) {
if(response == "error-email-exists"){
$('#errorMsg').html("An account is already assosciated with the email adress you entered.");
$('#errorMsg').hide().stop(true,true).fadeIn(600);
}else if(response == "success"){
window.location = "index.php";
}else{
$('#errorMsg').html(response);
$('#errorMsg').hide().stop(true,true).fadeIn(600);
}
}
});
PHP
<?php
session_start();
include("conn.php");
$post_firstName = $_POST['firstName'];
$post_lastName = $_POST['lastName'];
$post_dateofBirth = $_POST['dateofBirth'];
$post_gender = $_POST['gender'];
$post_propertyNo = $_POST['propertyNo'];
$post_propertyAddress = $_POST['propertyAddress'];
$post_streetAddress = $_POST['streetAddress'];
$post_locality = $_POST['locality'];
$post_email = $_POST['email'];
$post_password = $_POST['password'];
$checkexistsQuery = $conn->prepare('SELECT count(*) AS countExists FROM tbl_account acc WHERE acc.email = ?');
$checkexistsQuery->bind_param('s', $post_email);
$checkexistsQuery->execute();
$checkexistsQuery->store_result();
$checkexistsQuery->bind_result($countExists);
$checkexistsQuery->fetch();
if($countExists > 0){
echo 'error-email-exists';
}else{
$signupQuery = $conn->prepare('insert into tbl_account (name, surname, email, password, dob, genderID, propertyNumber, propertyAddress, streetAddress, localityID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$signupQuery->bind_param('ssssssssss', $post_firstName,$post_lastName,$post_dateofBirth,$post_gender,$post_propertyNo,$post_propertyAddress,$post_streetAddress,$post_locality,$post_email,$post_password);
if($signupQuery->execute()){
echo 'success';
}else{
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
?>
问题是没有读取“错误电子邮件存在”和“成功”消息。
如下重构代码。 使用json类型作为contentType
并向您的ajax添加error
方法。 使用json,您将变得更加灵活,并有机会添加错误,消息甚至标记等结构化数据。
JS:
$.ajax({
type: 'POST',
url: 'signup-user.php',
dataType: 'json',
data: $('form').serialize(),
success: function (response) {
if (response.error) {
$('#errorMsg').html(response.message);
$('#errorMsg').hide().stop(true, true).fadeIn(600);
} else {
if (response.exists) {
$('#errorMsg').html("An account is already assosciated with the email adress you entered.");
$('#errorMsg').hide().stop(true, true).fadeIn(600);
} else {
window.location = "index.php";
}
}
},
error: function (error) {
console.log(error);
}
});
PHP:
<?php
session_start();
include("conn.php");
$post_firstName = $_POST['firstName'];
$post_lastName = $_POST['lastName'];
$post_dateofBirth = $_POST['dateofBirth'];
$post_gender = $_POST['gender'];
$post_propertyNo = $_POST['propertyNo'];
$post_propertyAddress = $_POST['propertyAddress'];
$post_streetAddress = $_POST['streetAddress'];
$post_locality = $_POST['locality'];
$post_email = $_POST['email'];
$post_password = $_POST['password'];
$checkexistsQuery = $conn->prepare('SELECT count(*) AS countExists FROM tbl_account acc WHERE acc.email = ?');
$checkexistsQuery->bind_param('s', $post_email);
$checkexistsQuery->execute();
$checkexistsQuery->store_result();
$checkexistsQuery->bind_result($countExists);
$checkexistsQuery->fetch();
$response = [];
$response['error'] = false;
if($countExists > 0){
$response['exists'] = true;
}else{
$signupQuery = $conn->prepare('insert into tbl_account (name, surname, email, password, dob, genderID, propertyNumber, propertyAddress, streetAddress, localityID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$signupQuery->bind_param('ssssssssss', $post_firstName,$post_lastName,$post_dateofBirth,$post_gender,$post_propertyNo,$post_propertyAddress,$post_streetAddress,$post_locality,$post_email,$post_password);
if($signupQuery->execute()){
$response['exists'] = false;
}else{
$response['error'] = true;
$response['message'] = "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
}
echo json_encode($response);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.