title: Check if user exists with AJAX and PHP
body: <p>I am trying to create a signup form that checks if the user exists in the database, I inserted a sample user and when I tried signing up with that user it didn't say its already been taken. What have I done wrong?</p>
  
  <p>The JavaScript:</p>
  
  <pre><code>function formSubmit()
      {
      document.getElementById('email_valid').innerHTML = '';
  
      var temail=document.forms["signup_form"]["temail"].value.replace(/^\s+|\s+$/g, '');
  
  
        var atpos=temail.indexOf("@");
          var dotpos=temail.lastIndexOf(".");
          if (atpos&lt;1 || dotpos&lt;atpos+2 || dotpos+2&gt;=temail.length)
            {
            //alert("Not a valid e-mail address");
            setTimeout(function(){document.getElementById('email_valid').innerHTML = '&lt;br/&gt;Email must be valid...';},1000);
            var temailsub=0;
            }
            else
            {
            $.post('/resources/forms/signup/email.php',{email: temail}, function(data){
              document.getElementById('email_valid').innetHTML = data;
              if(data.exists){
                      document.getElementById('email_valid').innetHTML = '&lt;br/&gt;The email address you entered is already in use.';
                      var temailsub=0;
                  }else{
                      var temailsub=1;
                  }
              }, 'JSON');
            }
      if(temailsub==1e)
        {
          setTimeout(function(){document.getElementById("signup_form").submit();},1000);
        }
        else
        {
  
          return false;
        }
      }
  </code></pre>
  
  <p>The PHP file (email.php):</p>
  
  <pre><code>&lt;?php
  header('content-type: text/json');
  require_once $_SERVER['DOCUMENT_ROOT']."/resources/settings.php";
  $query = $pdo-&gt;prepare("SELECT * FROM users WHERE email=:email");
  $query-&gt;execute(array(
      ":email"=&gt; $_POST['email']
  ));
  echo json_encode(array('exists' =&gt; $query-&gt;rowCount() &gt; 0));
  ?&gt;
  </code></pre>
  
  <p>I have checked and double checked the code, I still cannot see why its not detecting that the email has already been used... what do i need to do to fix this and avoid this in the future?</p>