title: Comparing a XMLHTTP request with a string? Setting up a callback
body: <p>I'm a long time lurker and this is my first post so go easy on me...</p>
  
  <p>so I'm fairly new to Javascript, I'm going to get around to jQuery eventually but for now I'm sticking to regular Javascript.</p>
  
  <p>I've created a website in PHP and now I'm trying to setup some client side validation.</p>
  
  <p>So after a little research on these forums I've discovered that it isn't that easy to get hold of responseText because of it's asynchronous nature. So I looked into call backs but I'm having a lot of trouble setting them up. I've managed to set it up but it keeps returning the entire html page instead of just the echoed string.</p>
  
  <pre><code>function checkName(usernameString, callback) {
  
  //
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  
  xmlhttp.open("GET","checkName.php?q="+usernameString,true);
  xmlhttp.onreadystatechange=function()
  {
  
      if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
  {
  //document.getElementById("uMess").innerHTML=xmlhttp.responseText;
  
      callback(xmlhttp.responseText);
  
  
     }
  }
  //xmlhttp.open("GET","checkName.php?q="+usernameString,true);
  xmlhttp.send();
  
  
  
  } 
  
  
  
  function validateUsername(usernameString) {
   var valid = true;
  checkName(usernameString,function(err,response){
  if (err) {
      alert('Error: ' + err);
  
  } else {
      alert('Response: ' + response);
  } 
  });
  .....
  </code></pre>
  
  <p>Would someone mind pointing out where I'm going catastrophically wrong?? Thanks in advance guys :)</p>
  
  <p>Also here's the PHP file which checks if the username is taken or not</p>
  
  <pre><code>&lt;?php
      include_once 'header.php';
  
  
      $userName = $_GET['q'];
  
      if ( strlen($userName) &lt; 6 ){
  
     $response = "Too Short!";
     echo $response;
      }
  
      else{
      $query = "SELECT user FROM temporaryusers WHERE user='$userName'";
     $result = mysql_query($query);
     if(mysql_num_rows($result))
     {
      $response="Taken";
       echo $response;
     }
     else{
     $response="Available";
     echo $response;
     }
     }
     ?&gt;
  </code></pre>