4

Essentially, I'm trying to send an email to a PHP script to check for availability using ajax and an onKeyUp event in HTML.

So far I've tried about everything I can think of doing for the past several hours trying to troubleshoot this issue to no avail.

Note: In my source html, the script is in < head >.

I've attached my code below.

var checkEmail = function() {
  $.ajax({
    method: 'POST',
    url: 'checkEmail',
    dataType: 'json',
    data: {
      'email': $('#email').val()
    },
    success: function(response) {
      if (response == "0" || response == 0) {
        document.getElementById("email").style.border = "1px solid rgba(0, 255, 0, 0.9)";
        console.log("Good");
      } else if (response == "2" || response == 2) {
        document.getElementById("email").style.border = "1px solid yellow";
        console.log("Resp. bad");
      } else {
        document.getElementById("email").style.border = "1px solid rgba(255, 0, 0, 0.9)";
        console.log("Bad");
      }
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal-form-left">
  <input type="text" name="fname" placeholder="First Name" required><br>
  <input type="text" placeholder="Last Name" name="lname" required><br>
  <input type="email" placeholder="Email" id="email" onKeyUp="checkEmail();" required value=""><br>
  <input type="password" placeholder="Password" name="pass" id="password" onKeyUp='checkPass();' required pattern="(?=^.{6,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$"><br>
  <input type="password" placeholder="Confirm Password" id="confirm_password" onKeyUp="checkPass();" required pattern="(?=^.{6,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$"><br>
  <input type="submit" id="regButton" value="Register">
</div>

<?php
if(isset($_POST['email'])) {
    $email = $_POST['email']; 
    include("../src/php/config_users.php");

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $query = "SELECT email FROM info WHERE email=?";
    if ($stmt = $db_users->prepare($query)) {
        $stmt->bind_param("i", $email); //Make sure not to bind a string to an int
        $stmt->execute();
        $stmt->bind_result($email_rec);
        $stmt->fetch();
        if($stmt->num_rows >= "1") {
            echo "1";
            $stmt->close();
            $db_users->close();
            die();
        } else {
            echo "0";
            $stmt->close();
            $db_users->close();
            die();
        }
    }
} else {
    echo "2";
    die();
}

?>

2
  • 2
    You are expecting a JSON, thus jquery parses and returns an object, you are testing against strings. Maybe try datatype raw? Commented Nov 10, 2017 at 13:39
  • Url shoud be url: 'checkEmail.php', Commented Nov 10, 2017 at 13:41

1 Answer 1

4

The problem is most likely with the dataType attribute. You wrote json but are echoing a string.

You have two choices:

1) Either return a json like this (in php)

echo json_encode(array('result' => 1));

And then in javascript get the value like this

if (parseInt(response.result) === 1) {

2) The second option would be to change the dataType to html and then in the javascript I would suggest you use this code to compare:

if (parseInt(response) === 0) {

The === compares value and type (string, array, integer, etc). By using parseInt on the response, you're converting it to an integer making sure the comparison doesn't fail because you have a string.

Sign up to request clarification or add additional context in comments.

7 Comments

I've done the second option and my php is still only receiving 0.
@Brayden This means the problem is with your PHP code. You're comparing a >= with a string. Change this $stmt->num_rows >= "1" to this $stmt->num_rows >= 1. Make sure actually returns something by echoing the value and checking it.
I've now finished this and it seems that $_POST['email'] is not set because only 2 is being echoed.
Actually, my site isn't updating the javascript and dataType is still 'raw' from when i tested it.
@Brayden Try echoing what POST contains like this print_r($_POST);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.