我正在学习使用php的ajax工作方式,现在我发送请求;
$.ajax({
url: 'check_exists.php',
type: "POST",
data: registerForm.serialize(),
success: function(data) {
console.log(data)
registerMsg = $('.registerMsg');
if (data == 'nickname_exists') {
nickname.addClass('error');
} else if (data == 'email_exists') {
email.addClass('error');
} else {
registerMsg.html('');
}
}
});
现在发送到php文件并检查数据是否存在,如果我输入昵称,我回来nickname_exist
,如果我对电子邮件做同样的回复email_exists
。
但是现在,如果我同时获得这两个数据,就像nickname_existsemail_exists
这样的console.log,它不会触发if语句。
我从php文件发送,如;
require_once('db_connect.php');
if(isset($_POST['nickname'])){
$nickname = $_POST['nickname'];
$st = $db->prepare("SELECT * FROM users WHERE nickname=?");
$st->bindParam(1, $nickname);
$st->execute();
if($st->rowCount() > 0) { echo 'nickname_exists'; }
}
if(isset($_POST['email'])){
$email = $_POST['email'];
$st = $db->prepare("SELECT * FROM users WHERE email=?");
$st->bindParam(1, $email);
$st->execute();
if($st->rowCount() > 0) { echo 'email_exists'; }
}
我如何解决这个问题,以及如何以正确的方式处理ajax到php的方式,有人可以帮助我。
我需要将它设为console.log,如
nickname_exists
email_exists
INSTEAD OF
nickname_existsemail_exists
答案 0 :(得分:0)
使用商店变量并使用如下所示:
require_once('db_connect.php');
$what_exists = [];
if(isset($_POST['nickname'])){
$nickname = $_POST['nickname'];
$st = $db->prepare("SELECT * FROM users WHERE nickname=?");
$st->bindParam(1, $nickname);
$st->execute();
if($st->rowCount() > 0) { $what_exists[] = 'nickname_exists'; }
}
if(isset($_POST['email'])){
$email = $_POST['email'];
$st = $db->prepare("SELECT * FROM users WHERE email=?");
$st->bindParam(1, $email);
$st->execute();
if($st->rowCount() > 0) { $what_exists[] = 'email_exists'; }
}
echo json_encode($what_exists);
将该变量输出为JSON并对AJAX调用进行更改,如:
$.ajax({
url: 'check_exists.php',
type: "POST",
data: registerForm.serialize(),
dataType: 'json',
success: function(data) {
console.log(data)
registerMsg = $('.registerMsg');
if ($.inArray(data, 'nickname_exists')) {
nickname.addClass('error');
} else if ($.inArray(data,'email_exists')) {
email.addClass('error');
} else {
registerMsg.html('');
}
}
});
答案 1 :(得分:0)
将结果收集到如下数组中:
require_once('db_connect.php');
//Initalize the array
$result = array(
'nickname_exists' => 0,
'email_exists' => 0
);
if(isset($_POST['nickname'])){
$nickname = $_POST['nickname'];
$st = $db->prepare("SELECT * FROM users WHERE nickname=?");
$st->bindParam(1, $nickname);
$st->execute();
if($st->rowCount() > 0) {
$result['nickname_exists'] = 1;
}
}
if(isset($_POST['email'])){
$email = $_POST['email'];
$st = $db->prepare("SELECT * FROM users WHERE email=?");
$st->bindParam(1, $email);
$st->execute();
if($st->rowCount() > 0) {
$result['email_exists'] = 1;
}
}
//Gives back the result in JSON.
echo json_encode($result);
然后用json返回。 在此之后,您可以在javascript中检查所有内容:
//Initialize the hasError
var hasError = false;
if (data.nickname_exists == 1) {
//Set error for nickname
nickname.addClass('error');
hasError = true;
}
if (data.email_exists == 1) {
//Set error for email
email.addClass('error');
hasError = true;
}
//If we had no error:
if (!hasError) {
registerMsg.html('');
}
答案 2 :(得分:0)
您需要以json格式返回数据
echo json_encode(array('nickname_exists'=>1,'email_exists'=>1)); //here 1 is for true and 0 if for false
在JS中你需要写这个代码
$.ajax({
url: 'check_exists.php',
type: "POST",
data: registerForm.serialize(),
success: function(data) {
parsedData = jQuery.parseJSON(data);
registerMsg = $('.registerMsg');
if (parsedData.nickname_exists != 1) {
nickname.addClass('error');
} else if (parsedData.email_exists !=1) {
email.addClass('error');
} else {
registerMsg.html('');
}
}
});
答案 3 :(得分:0)
$.ajax({
url: 'check_exists.php',
type: "POST",
data: registerForm.serialize(),
success: function(data) {
myfunction(data);
}
});
function myfunction(data){
console.log(data)
registerMsg = $('.registerMsg');
if (data == 'nickname_exists') {
nickname.addClass('error');
} else if (data == 'email_exists') {
email.addClass('error');
} else {
registerMsg.html('');
}
}