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
echo 'nickname_exists\n\r';
echo "email_exists\n\r";
– Dawid O Nov 19 '14 at 11:55