title: jQuery Validator Plugin - check for existing Username/Email in mysql database
body: <p>I've successfully created a form that submits and adds users to a mysql database, and form validation with <a href="http://jqueryvalidation.org/" rel="noreferrer">'jQuery Validator' plugin</a> works great for everything except checking to see if the username already exists in the database... </p>
  
  <p>I've just spent about 8 hours reading and trying to figure out a way to define a new method with the 'jQuery Validator' plugin. I just can't seem to understand how I would go about checking the database for the  entered username or email and return whether it already exists or not using jQuery.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;script src="../assets/js/main.js"&gt;&lt;/script&gt;
  &lt;script src="../assets/js/ajax.js"&gt;&lt;/script&gt;
  &lt;script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"&gt;&lt;/script&gt;
  &lt;!-- FORM VALIDATION --&gt;
  &lt;script type="text/javascript"&gt;
  
  
      jQuery.validator.addMethod("checkExists", 
  function(value, element) {
      //No idea what to call here
  }, 
  "Username already exists."
  );
  
  
          //&lt;![CDATA[
          $(window).load(function(){
          $("form").validate({
          rules: {
              username: {minlength: 3, required: true, checkExists: true},
              email: {email: true, required: true},
              pass1: {minlength: 3, required: true},
              pass2: {minlength: 3, required: true, equalTo: "#pass1"},
              country: {required: true},
              tandc: {required: true},
          },
          messages: {
          username:   {required: "You need to enter a Username."},
          email:      {required: "You need to enter an Email Address."},
          pass1:      {required: "You need to enter a Password."},
          pass2:      {required: "You need to enter your password again.", equalTo: "Your passwords don't match."},
          country:    {required: "You need to tell us where you live."},
          tandc:      {required: "You need to read and agree to the Terms and Conditions to use CGE."}
          },
  
  
          showErrors: function(errorMap, errorList) {
          $.each(this.successList, function(index, value) {
          return $(value).popover("hide");
          });
          return $.each(errorList, function(index, value) {
          var _popover;
          console.log(value.message);
          _popover = $(value.element).popover({
          trigger: "manual",
          placement: "right",
          content: value.message,
          template: "&lt;div class=\"popover\"&gt;&lt;div class=\"arrow\"&gt;&lt;/div&gt;&lt;div class=\"popover-inner\"&gt;&lt;div class=\"popover-content\"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"
          });
          _popover.data("popover").options.content = value.message;
          return $(value.element).popover("show");
          });
          }
          });
          });//]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>If anyone clever could please amend my code to show me how I should have done it, it would be a great help - I feel like I'm about to go crazy!</p>
  
  <p>Thanks in advance, can't wait to see the solution :-)</p>
  
  <hr>
  
  <h2><em><strong>EDIT - This is my current code</em></strong></h2>
  
  <p>Nothing seems to happen at all, I feel like I'm closer though:</p>
  
  <p>CURRENT CODE:</p>
  
  <p><em>signup.php</em></p>
  
  <pre><code>    $(window).load(function(){
              $("form").validate({
              rules: {
                  username: {minlength: 3, required: true},
                  email: {email: true, required: true, remote: {url: "./validation/checkUnameEmail.php", type : "post"}},
                  pass1: {minlength: 3, required: true},
                  pass2: {minlength: 3, required: true, equalTo: "#pass1"},
                  country: {required: true},
                  tandc: {required: true}
  },
  </code></pre>
  
  <p><em>checkUnameEmail.php</em></p>
  
  <pre><code>&lt;?php
      include_once(".../php_includes/db_conx.php");
      $email = urldecode($_POST['email']);
      $result = mysqli_query($db_conx, "SELECT * FROM users WHERE email = '$email' LIMIT 1;");
      $num = mysqli_num_rows($result);
      if($num == 0){
          echo "true";
      } else {
          echo "E-Mail-Adresse schon registriert.";
      }
      mysqli_close($db_conx);
  ?&gt;
  </code></pre>
  
  <p>*db_conx.php*</p>
  
  <pre><code>&lt;?php
  $db_conx = mysqli_connect("localhost", "root", "root", "membership");
  //Evlauate the connection
  if (mysqli_connect_errno()){
      echo mysqli_connect_error();
      exit();
  }
  ?&gt;
  </code></pre>