title: Using Javascript to run a php script to check username
body: <p>I've been trying to build a registeration form for a website I am building. I can do the basics but I want it to check the username availability without reloading the page.</p>
  
  <p>JAVASCRIPT</p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
  &lt;script&gt;
      $(document).ready(function()
      {
          $("#Username").focusout(function()
          {
              //Check if usernane if available
              var username = $("#Username").val();
              $.post("scripts/check_username.php", {username:  username}, function(data)
              {
                  if(data == 'false')
                  {
                      alert('Username not available');
                      $("#Username").setCustomValidity("This username is already taken!");
                  }
                  else
                  {
                      alert('Username available');
                  }
              });
              return false;
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;form id="registerForm"&gt;
                  &lt;table&gt;
                      &lt;tr&gt;&lt;td&gt;Username&lt;/td&gt;&lt;td&gt;&lt;input id="Username" class='textInput' type='text' name='username' required&gt;&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>PHP SCRIPT</p>
  
  <pre><code>&lt;?php
  include 'open_connection.php';
  $result = 'true';
  $username = mysql_real_escape_string($_POST['username']);
  
  $result = mysql_query("SELECT * FROM tblMembers WHERE Username='$username'");
  while($row = mysql_fetch_array($result))
  {
      $result = 'false';
  }
  echo $result;
  ?&gt;
  </code></pre>
  
  <p>When I leave the textbox it says username available no matter what. I placed a username "test" in the database... no luck</p>
  
  <p>Please help</p>