title: Using AJAX and PHP to change onclick
body: <p>I get the following errors when I run this simple AJAX code</p>
  
  <ul>
  <li>uncaught syntaxerror unexpected token</li>
  <li>uncaught reference error loaddoc is not defined .............when I press the button</li>
  </ul>
  
  <p>From reading stackoverflow I am pretty sure my problem is with the 'onclick' but I cannot understand how to use the listener instead which I see is recommended to do.</p>
  
  <p>My AJAX code is </p>
  
  <pre><code>&lt;body&gt;
  
      &lt;a&gt;Hello-first&lt;/a&gt;
  
      &lt;p id="trainblock"&gt;Can I change this&lt;/p&gt;
  
      &lt;button type="button" onclick="loaddoc()"&gt;Press&lt;/button&gt;
  
      &lt;script&gt;
          function loaddoc()
          {
              var xmlhttp=new XMLHttpRequest();
              xmlhttp.onreadystatechange=function();
              if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) {
                  document.getElementById("trainblock").innerHTML=xmlhttp.responseText;
              }
              xmlhttp.open("GET","gettrains.php",true);
              xmlhttp.send();
          }
      &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>