gpt4 book ai didi

javascript - Ajax PHP - 检查从 php 返回的响应/数据值

转载 作者:行者123 更新时间:2023-12-02 22:55:40 24 4
gpt4 key购买 nike

基本上,我正在验证注册表单并使用 ajax 调用 php 脚本来检查电子邮件地址是否已存在,否则将执行查询。如果电子邮件地址已经存在(使用计数查询进行检查),我将向 ajax 函数回显错误(通过响应) - 但它不会被读取。

我尝试研究类似的主题,但没有成功。

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);
?>

关于javascript - Ajax PHP - 检查从 php 返回的响应/数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com