I have a simple form for submitting an email address to a database. I want to check first, using javascript and AJAX, if that email address already exists in the table. The code I am using always returns true regardless if the row already exists in the database. Any hints?
JS Code
<script> 
$('form').submit(function(e) 
{
  alert('submit intercepted');
  e.preventDefault(e);
  var u = $('#email').val();
  $.ajax({
      url: './test2.php',
      type: 'POST',
      data: u,
      success: function (response) {
      //get response from your php page (what you echo or print)
        $('#status').append('<p>The email is ok to use and the row has been inserted </p><p>' + response);
        console.log('Submitted');
      },
      error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
        alert('Email exists');
      }
    });
});
</script>
HTML Code
<form id="form" action="./test2.php" method="post"> 
    E-mail: <input type="email" name="email" id="email" required><br>
    <span id="status">   </span>
    <input type="submit" id="btnsub">
  </form>
PHP Code
if(isset($email)) {
  $sql_email_check = $conn->query("SELECT Email FROM test WHERE Email='$email' LIMIT 1") ;
  $email_check = $sql_email_check->num_rows;
  if ($email_check < 1) {
    //return "Available";
    return true;   
    //header('Location: /test.php');
    exit();
  } 
  else 
  {
    //return "Unavailable";
    return false;
    //header('Location: /test.php');
    exit(); 
  }
}
 
     
    