I'm learning the way to work with ajax with php now i send request with;
$.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('');
}
}
});
Now this send to the php file and checks if data exists if i input nickname i get back nickname_exist
back and if i do the same with the email i get email_exists
back.
But now if i get both data it console.log like nickname_existsemail_exists
this way it doesn't trigger the if statement.
i send from php file like;
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'; }
}
How do i fix this, and is the way how i handle ajax to php the right way, can somebody help me a hand.
I need to make it console.log like
nickname_exists
email_exists
INSTEAD OF
nickname_existsemail_exists
Collect your results into an array like this:
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);
Then return back with the json. After this you can check all of them in your 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('');
}
Use a store variable and use that like below:
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);
Output that variable as JSON and make a change to AJAX call like:
$.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('');
}
}
});
You need to return data in json format
echo json_encode(array('nickname_exists'=>1,'email_exists'=>1)); //here 1 is for true and 0 if for false
IN JS YOU NEED TO WRITE THIS CODE
$.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('');
}
}
});
$.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('');
}
}