title: TypeError: Cannot read property 'documentElement' of null PHP/AJAX chat
body: <p>I'm following an PHP/AJAX chat tutorial in the "Building Responsive Web Applications AJAX and PHP" book and i'm encountering the following error :</p>
  
  <pre><code> TypeError: Cannot read property 'documentElement' of null
  </code></pre>
  
  <p>I'm fairly sure it's refering to this code in the javascript file :</p>
  
  <pre><code>function readMessages(){
      var response = XmlHttpGetMessage.responseText;
  
      if(response.indexOf("ERRNO") &gt;= 0 || response.indexOf("error:")&gt;= 0 || response.length==0){
          throw(response.length == 0 ? "Void server response." : response);
      }   
  
      response = XmlHttpGetMessage.responseXML.documentElement;
  
      clearChat = response.getElementByTagName("clear").item(0).firstChild.data;
  
      if(clearChat == "true"){
          $("#scroll").html("");
          lastMessageID = -1;
      }
  
      idArray = response.getElementByTagName("id");
      //nameArray = response.getElementByTagName("name");
      timeArray = response.getElementByTagName("time");
      messageArray = response.getElementByTagName("message");
  
      displayMessages(idArray, nameArray, timeArray, messageArray);
  
      if(idArray.length&gt;0){
          lastMessageID = idArray.item(idArray.length - 1).firstChile.data;
          setTimeout("requestNewMessages();", updateInterval);
      }
  }
  </code></pre>
  
  <p>This is the ajax request I send :</p>
  
  <pre><code>if(cache.length&gt;0){
      params = cache.shift();
  }else{
      params="mode=RetrieveNew" +
             "$id=" +lastMessageID;
  }
  
  XmlHttpGetMessage.open("POST", chatURL, true);
  XmlHttpGetMessage.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  XmlHttpGetMessage.onreadystatechange = handleRecievingMessages;
  XmlHttpGetMessage.send(params);
  </code></pre>
  
  <p>And here is the PHP file :</p>
  
  <pre><code>&lt;?php
  require_once('init.php');
  
  
  $mode = $_POST['mode'];
  id=0;
  $chat = new Chat();
  if($mode=='SendAndRetrieveNew'){
      $user_id = $_POST['user_id'];
      $message = $_POST['message'];
      $color = $_POST['color'];
      $id = $_POST['id'];
  
      if($user_id != '' &amp;&amp; $message != '' &amp;&amp; $color != ''){
          $chat-&gt;postNewMessage($user_id, $message, $color);
      }
  }elseif($mode=='DeleteAndRetrieveNew'){
      $chat-&gt;deleteAllMessages();
  }elseif($mode=='RetrieveNew'){
      $id = $_POST['id'];
  }
  
  if(ob_get_length()){ob_clean();}
  
  header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
  header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . 'GMT'); 
  header('Cache-Control: no-cache, must-revalidate'); 
  header('Pragma: no-cache');
  header('Content-Type: text/xml');
  
  echo $chat-&gt;getNewMessages($id);
  ?&gt;
  </code></pre>
  
  <p>Here is the handle receiving messages function
      function handleRecievingMessages(){</p>
  
  <pre><code>    if(XmlHttpGetMessage.readyState == 4){
          if(XmlHttpGetMessage.status == 200){
                  try{
  
                      readMessages();
                  }catch(e){
                      displayError(e.toString());
                  }   
              }else{
                  displayError(XmlHttpGetMessage.statusText);
              }
          }
      }
  </code></pre>
  
  <p>And finally my getNewMessages function</p>
  
  <pre><code>public function getNewMessages($id=0){
              $queryArray = array($id);
               if($id&gt;0){ 
          //only get the new messages
          $query = 
          ' SELECT message_id, user_id, message, DATE_FORMAT(posted_on, "%H:%i:%s") 
          AS posted_on FROM chat WHERE message_id &gt; ' 
          . $id . 
          ' ORDER BY message_id ASC'; 
          }else{
          //on the first load only retrieve the last 50 messages from server
          $query = 
          ' SELECT message_id, user_id, message, posted_on 
          FROM (SELECT message_id, user_id, message,s DATE_FORMAT(posted_on, "%H:%i:%s") 
          AS posted_on FROM chat ORDER BY message_id DESC LIMIT 50) AS Last50 
          ORDER BY message_id ASC';
          } 
  
              $query = $this-&gt;_database-&gt;_link-&gt;prepare($queryString);
              $query-&gt;execute();
  
              $response = '&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;';
          $response .= '&lt;response&gt;';  
          $response .= $this-&gt;isTableCleared($id); // output the clear flag
  
          // if we have any results, loop through all the fetched messages to build the result message
          if($query-&gt;rowCount()){ //get number of rows in a result, aka check for results
          while ($row = $query-&gt;fetch(PDO::FETCH_ASSOC)){ //get results as an associative array
          $id = $row['message_id'];
          $userName = $row['user_id'];
          $time = $row['posted_on'];
          $message = $row['message'];
          $response .= '&lt;id&gt;' . $id . '&lt;/id&gt;' . 
                       '&lt;color&gt;' . $color . '&lt;/color&gt;' . 
                       '&lt;time&gt;' . $time . '&lt;/time&gt;' . 
                       '&lt;name&gt;' . $userName . '&lt;/name&gt;' . 
                       '&lt;message&gt;' . $message . '&lt;/message&gt;';
        }
  
        $query-&gt;close(); // close the database connection
      }
      $response .= '&lt;/response&gt;';
      return $response;
          }
  </code></pre>
  
  <p>When I debug with firebug i get the following text response :</p>
  
  <pre><code>    "&lt;br /&gt; &lt;b&gt;Parse error&lt;/b&gt;: syntax error, unexpected ';', expecting T_FUNCTION in `&lt;b&gt;C:\xampp\htdocs\thinkaztech\includes\chat.class.php&lt;/b&gt; on line &lt;b&gt;93&lt;/b&gt;&lt;br /&gt; "`
  </code></pre>
  
  <p>However I do not have a line 93?</p>
  
  <p>I'm not sure what the error is, i'm connected to the database just fine and my chat.class.php file seems to be being called correctly. Thanks for any helpful advice in advance!</p>