Jump to content

getElementsByTagName not finding desired tag


helloise

Recommended Posts

i have this part of code in JS:

 

     var receiveReq = getXmlHttpRequestObject();
     function getXmlHttpRequestObject() 
     {
          if (window.XMLHttpRequest)
         {
              return new XMLHttpRequest();
         }
     }


function getChatText()
{
      if (receiveReq.readyState == 4 || receiveReq.readyState == 0) 
      {                 
           receiveReq.open("GET","getChat_xml.php?chat=1&last=7", true);
           receiveReq.onreadystatechange = handleReceiveChat; 
           receiveReq.send();
      }
}

function handleReceiveChat() 
{
     if (receiveReq.readyState == 4)
     {
          var chat_div = document.getElementById('div_chat');
          var xmldoc = receiveReq.responseXML;
          var message_nodes = xmldoc.getElementsByTagName("message");
      }
}

 

then in my getChat_xml.php i have:

 

$xml = '<?//xml version="1.0" ?><root>';
$last = 7;
$sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time" . 
	" FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id = " . $last;
$message_query = db_query($sql);

while($message_array = db_fetch_array($message_query))
{
    $xml .= '<message id="' . $message_array['message_id'] . '">';
    $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>';
    $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>';
    $xml .= '<time>' . $message_array['post_time'] . '</time>';
    $xml .= '</message>';
}

$xml .= '</root>';
echo $xml;

 

i get a "cannot cal a method getElementsByTagName of null" ???

 

i have no idea whats going on, help please?

thanks

i would start by alerting the contents of "receiveReq.responseXML" to confirm the correct results.

 

function handleReceiveChat() 
{     
if (receiveReq.readyState == 4)     
{         
var chat_div = document.getElementById('div_chat');          
var xmldoc = receiveReq.responseXML;
alert(xmldoc);         
var message_nodes = xmldoc.getElementsByTagName("message");      
}}

 

my hunch here is that you will want responseText and not responseXML, is your php page content type text/plain or text/xml?

in my getChat_xml.php:

   header("Content-Type: text/xml; charset=utf-8");

 

i did do alert(xmldoc) and it returns null

 

i changed the responsXML to responsText and now have values for xmldoc:

<?//xml version="1.0" ?><root><message id="7"><user>Me, ME, me and mE </user><text>dfhdfgdfg</text><time>11:04</time></message></root>  dfgdfg

 

yipeee now to get the values..

 

thanks alot

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.