title: Validate form, check if user exists, register them. All with Ajax
body: <p>My javascript is inside an html file called register.html.
  The user submits the form. This should then trigger the <code>$('input[name="createacc"]').click(function (e)</code> AJAX then sends those 4 variables to checkuser.php. Checkuser.php should then check to see if the username exists. If it does exist, it should echo 0. If it does not exists, it should echo 1. Register.html then checks to see what checkuser.php echoed. If the echo was "0" then, then an alert box should appear saying username unavailable. If the echo was "1" or anything else, register.html should run <code>$("#registerform").submit();</code> which then does the php script. This should all happen without leaving the register.html page.</p>
  
  <p><strong>I check chrome's built in debugger and I see that if the account exists checkuser.php writes back 0 and if the account doesn't it writes back 1. But for some reason nothing else happens. The account does not register nor do I get an alert box saying the username is unavailable</strong></p>
  
  <p>here is my register.html</p>
  
  <pre><code>&lt;form ata-parsley-validate name="registerform" id="registerform" action="register.php" method="post"&gt;
  
                          &lt;p&gt;
                              &lt;label for="firstname"&gt;First Name:&lt;/label&gt;
                              &lt;input name="firstname" id="firstname" maxlength="32" type="text" placeholder="Optional" /&gt;
                          &lt;/p&gt;
                          &lt;p&gt;
                              &lt;label for="username" id="usernameText"&gt;Username:&lt;/label&gt;
                              &lt;input data-parsley-pattern="^[A-Za-z0-9_]{3,15}$" data-parsley-length="[3, 15]" name="username" id="username" maxlength="32" type="text" data-parsley-error-message="Username needs to be between 3 and 15 characters. Case sensitive. No special characters allowed." required/&gt;
                          &lt;/p&gt;
                          &lt;p&gt;
                              &lt;label for="password1"&gt;Password:&lt;/label&gt;
                              &lt;input name="password1" id="password1" data-parsley-pattern="^[A-Za-z0-9_-]{5,25}$" data-parsley-length="[5, 25]" type="password" data-parsley-equalto="#password2" data-parsley-error-message="Passwords must match. Needs to be between 5 and 25 characters. Case sensitive. No special characters allowed." required/&gt;
                          &lt;/p&gt;
                          &lt;p&gt;
                              &lt;label for="password2"&gt;Confirm Your Password:&lt;/label&gt;
                              &lt;input name="password2" id="password2" data-parsley-length="[5, 25]" data-parsley-error-message="Passwords must match. Needs to be between 5 and 25 characters. Case sensitive. No special characters allowed." data-parsley-pattern="^[A-Za-z0-9_-]{5,25}$" type="password" data-parsley-equalto="#password1" required/&gt;
                          &lt;/p&gt;
                          &lt;p&gt;
                              &lt;label for="email"&gt;E-Mail:&lt;/label&gt;
                              &lt;input data-parsley-trigger="change" name="email" id="email" maxlength="1024" type="email" required/&gt;
                          &lt;/p&gt;
                          &lt;p&gt;
                              &lt;input type="submit" id="submit" class="submit" name="createacc" value="Register" /&gt;
                          &lt;/p&gt;
                      &lt;/form&gt;
  </code></pre>
  
  <p>Here is my javascript</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
  
  
      $('input[name="createacc"]').click(function (e) {
          var username = $('input[name="username"]').val();
          var firstname = $('input[name="firstname"]').val();
          var password1 = $('input[name="password1"]').val();
          var email = $('input[name="email"]').val();
          e.preventDefault();
         $.ajax({
              type: 'POST',
              data: {
                  username: username,
                  firstname: firstname,
                  password1: password1,
                  email: email
              },
              url: 'checkuser.php',
              success: function (data) { //Receives the data from the php code
                  if (data === "0") {
                      alert("Username Unavailable");
                  } else {
                     $("#registerform").submit();
                  alert("Account successfuly created"); 
                  }
  
  
              },
              error: function (xhr, err) {
                  console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                  console.log("responseText: " + xhr.responseText);
              }
          });
      });
  
          });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Update - I have fixed parts of my code through the help of others below me. My only issue now is that <code>$("#registerform").submit();</code> doesn't do anything</strong></p>