PHP validator gets 0 from AJAX

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

So far I have tried everything that I could think of over the past few hours, trying to fix this problem to no avail.

Note. In my original html script is in <head>.

I have 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>
Run codeHide result
<?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();
}

? >

+4
source share
1 answer

The problem is most likely related to the attribute dataType. You wrote json, but repeat the line.

You have two options:

1) Either return json like this (in php)

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

And then in javascript get a value like this

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

2) - dataType html, javascript :

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

=== (, , ..). parseInt , , , , .

+4

Source: https://habr.com/ru/post/1689068/


All Articles