title: Use AJAX and PHP to "Alert" the user if the username exists on the same page?
body: <p>I have found many AJAX scripts that do a live check on if the username exists in a MySQL database. For example, most will maybe show a green check image if it does not exist or a red x if it does already exist.</p>
  
  <p>I am interested in a slightly different method for this form.</p>
  
  <p>Users fill out a 10 question quiz and then are taken to a user registration form. I only want to insert the user's answers into the database if they complete the quiz.</p>
  
  <p>Currently, if a user enters a username or email that already exists, they will receive an error on the next page and be told to be go back only to find that the form has been reset.</p>
  
  <p>That is why I want the information validated all on the same page.</p>
  
  <p>Upon clicking the submit button, a javascript function is called that verifies a few things such as if the user has entered a date of birth, has not left a form blank, if passwords match, etc.</p>
  
  <p>It returns false if any of the criteria is not met so that the form does move to the next page unless all of the functions return true.</p>
  
  <p>Here is what it looks like.</p>
  
  <pre><code>function checkForm() {
  
  if (checkUser() &amp;&amp; checkPassword() &amp;&amp; checkMonth() &amp;&amp; checkDay() &amp;&amp; checkAddress() &amp;&amp; checkYear()) {
      document.getElementById("quizForm").method="post";
      document.getElementById("quizForm").action="register.php";
  }
  else {
      return false;
  }
  }
  </code></pre>
  
  <p>I am interested in creating a username/email check function that uses ajax to access a php page that searches the database and returns true or false to javascript on if the username/email exists in the database.</p>
  
  <p>That way, I can just use the old javascript alert to say if a username/email exists and then return false so that the form is not submit.</p>
  
  <p>This is an example of how I am writing the functions for this:</p>
  
  <pre><code>function checkPassword() {
  var pass1 = document.getElementById("pass").value;
  var pass2 = document.getElementById("c_pass").value;
  var pass1l = pass1.length;
  
  if (pass1l &lt; 5) {
      alert("Please create a password that is longer than 5 characters.");
      return false;
  }
  else {
      if (pass1 != pass2) {   
          alert("Your passwords do not match.");
          return false;
  }
      else {
          return true;
      }
  }
  
  }
  </code></pre>
  
  <p>Can anyone point me in the right direction for this? I have been searching around but have not found anything that is this specific.</p>
  
  <p>Thank you.</p>