简体   繁体   中英

jQuery AJAX check if email exists in database not working for some reason

I am trying to use jQuery, AJAX, PHP, and MySQL to check if an email entered into a form already exists in a database. This is my current jQuery code :

$.post('check-email.php', {'suEmail' : $suEmail}, function(data) {
  if(data=='exists') {
    validForm = false;
    $suRememberMeCheckbox.css('top', '70px');
    $suRememberMeText.css('top', '68px');
    $signUpSubmit.css('top', '102px');
    $tosppText.css('top', '115px');
    $suBox.css('height', '405px');
    $suBox.css('top', '36%');
    $errorText.text('The email has been taken.');
    return false;
  };
});

And this is my PHP code:

<?php include("dbconnect.php") ?>
  <?php
    $sql = "SELECT email FROM users WHERE email = '" .$_POST['suEmail'] . "'";
    $select = mysqli_query($connection, $sql);

    if (mysqli_num_rows($select) > 0) {
      echo "exists";
    }
?>

When I go through with the sign up form, when I use an email already in the database, the error text never changes to what I specified, but instead to some other error cases I have coded. Why is this not working! Thanks so much!

You may have extra whitespace in response

Try:

if(data.trim()=='exists') {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM