Javascript Ajax PHP-检查PHP返回的响应/数据值
基本上,我正在验证一个注册表单,并使用ajax调用一个php脚本来检查电子邮件地址是否已经存在,或者执行查询。如果电子邮件地址已经存在(通过计数查询进行检查),我将向ajax函数回显一个错误(通过响应)——但是它没有被读取 我试着研究类似的主题,但没有成功 阿贾克斯 phpJavascript Ajax PHP-检查PHP返回的响应/数据值,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,基本上,我正在验证一个注册表单,并使用ajax调用一个php脚本来检查电子邮件地址是否已经存在,或者执行查询。如果电子邮件地址已经存在(通过计数查询进行检查),我将向ajax函数回显一个错误(通过响应)——但是它没有被读取 我试着研究类似的主题,但没有成功 阿贾克斯 php 问题在于“错误电子邮件存在”和“成功”消息未被读取。按照以下方式重构代码。将json类型用作contentType,并将error方法添加到ajax中。使用json,您将更加灵活,并有机会添加结构化数据,如错误、消息甚至
问题在于“错误电子邮件存在”和“成功”消息未被读取。按照以下方式重构代码。将json类型用作
contentType
,并将error
方法添加到ajax中。使用json,您将更加灵活,并有机会添加结构化数据,如错误、消息甚至标记
js:
php:
你说“不被阅读”是什么意思?具体发生了什么?如果在控制台.log(response)
之前确实有一行新行,那么成功的结果是什么?在成功的旁边写入阻止错误:函数(error){console.log(error)}
阻止,以确保输入了成功的结果。检查是否没有返回空格,因为这将中断您的响应==“存在错误电子邮件”
检查。因此,返回序列化响应格式是更好的做法。快速修复方法是响应。trim()==='error email exists'
,但是我建议使用JSON来完全避免问题。编辑:这不会相应地起作用,并返回:on consoleSorry。输入错误缺少$
->echo json\u encode($response)代码>。我更新了答案。谢谢!一个简单的问题-如果错误由error:function(error)处理,那么响应['error']是否含糊不清?否。这个名称由您决定<代码>功能(错误)
在响应状态不是200(服务器错误,例如状态500)时触发。
$.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);
}
}
});
$.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
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);
?>