title: jquery - form ajax database check for unique value
body: <p>using</p>
  
  <ul>
  <li>jquery validator plugin</li>
  <li>jquery </li>
  </ul>
  
  <p>try to:</p>
  
  <ul>
  <li>make sure a username doesn't exist in the database
  <ul>
  <li>have a form + a field + remote check</li>
  <li>i want to let jquery check using ajax if it exists.</li>
  </ul></li>
  </ul>
  
  <p>what works:</p>
  
  <ul>
  <li>check-username.php does work by itself</li>
  </ul>
  
  <p>what doesn't</p>
  
  <ul>
  <li>form doesn't seem to be able to pass return boolean value to validate the remote jquery validation</li>
  <li>how do I get it to pass the value so that the field can be validated?</li>
  </ul>
  
  <p><strong>HTML</strong>
       
        
      
          
          </p>
  
  <pre><code>    &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js" type="text/javascript"&gt;&lt;/script&gt; 
      &lt;script language="javascript" src="validate.js"&gt;&lt;/script&gt;
  
      &lt;meta name="generator" content="" /&gt;
      &lt;style type="text/css"&gt;&lt;/style&gt;
    &lt;/head&gt;
  &lt;body&gt;
  
  &lt;form action="/" method="post" id="register-form"&gt;
      &lt;div&gt;
          &lt;label for="username"&gt;Username&lt;font color="red"&gt;*&lt;/font&gt;:&lt;/label&gt; 
          &lt;input type="text" name="username" size="20" id="username" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;input type="submit" name="submit" value="submit" /&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>JQUERY Validate</strong></p>
  
  <pre><code>$().ready(function() {
      // validate signup form on keyup and submit
      $("#register-form").validate({
          rules: {
              username: {
                  required: true,
                  remote: "check-username.php"
                  },
          },
          messages: {
              username:{
                  remote: "This username is already taken! Try another."
              },
          },
      });
  });
  </code></pre>
  
  <p><strong>Check-USERNAME.php</strong></p>
  
  <pre><code>&lt;?php
  
  $searchVal = $_POST['username'];
  
  try {
      $dbh = new PDO("mysql:host=$hostname;dbname=$data", $username, $password);
  
      $sql = "SELECT * FROM users WHERE USERNAME = " . "'" . $searchVal .  "'";
      $stmt = $dbh-&gt;query($sql);
      $result = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
     if($result&gt;0){
       return true;
     }else {
       return false;
     }
  
      $dbh = null;
      }
          catch(PDOException $e)
      {
          echo $e-&gt;getMessage();
      }       
  ?&gt;
  </code></pre>