title: AJAX - Retrieving data using JavaScript XMLHttpRequest and PHP
body: <p>I am trying to use <strong>AJAX</strong> on a <strong>form</strong> so that the user doesn't have to leave the current page to submit their name. I have got the function to work when the user types their name; it changes automatically while they're typing. What I'm struggling with is the <strong>onClick</strong> to submit the name. I am not receiving any response from the PHP file and I do not know why. Here is my code:</p>
  
  <p>The form page: (The submit function sends the name through a global variable via the GET request)</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  //Decalre the variables of name as Global variable
  var name;
  
      //For IE 5 + 6
  function httpReq(){
      if (window.XMLHttpRequest) { 
          // code for IE7+, Firefox, Chrome, Opera, Safari
          return xmlhttp=new XMLHttpRequest();
      } else { 
          // code for IE6, IE5
          return xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
  }
  
  function nameTest(str) {
      if (str.length==0) { 
          document.getElementById("nameTest").innerHTML="Please enter your name.";
          return;
      }
      httpReq();
      xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
              document.getElementById("nameTest").innerHTML=xmlhttp.responseText;
          }
      }   
      xmlhttp.open("GET","nameTest.php?q="+str,true);
      xmlhttp.send();
      return name = str;
  }
  
  
  // function to submit the users details to the database
  function submit(str) {
      httpReq();
      xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
              document.getElementById("submitted").innerHTML=xmlhttp.responseText;
          }
      }   
      xmlhttp.open("GET","mailing-list.php?name="+name, true);
      xmlhttp.send();
  }   
  &lt;/script&gt;
  
      Name: &lt;input type="text" onkeyup="nameTest(this.value)"&gt;
      &lt;span id="nameTest"&gt;Please enter your name.&lt;/span&gt;
              &lt;input style="width: 100px; height: 40px;" type="button" onclick="submit" value="Join!"&gt;
          &lt;span id="submitted"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>The <strong>PHP</strong> page which doesn't give a response (Just echoing something simple until I get a response):</p>
  
  <pre><code>&lt;?php
  
  $name = $_GET["name"];
  
  echo "Hello There " + $name;
  
  ?&gt;
  </code></pre>
  
  <p>Can't seem to figure out why there is no response. Any help appreciated!</p>