Jump to content

[SOLVED] Output Error object HTMLCollection


BillyBoB

Recommended Posts

I am creating a chat script for my upcoming game and when I send for the username/time/message it fills all three variables with object HTMLCollection.. Here's the code.

 

javascript:

var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;

function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
	return new XMLHttpRequest();
} else if(window.ActiveXObject) {
	return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function getMessages(chatRoomId, divId) {
obj = document.getElementById(divId);

if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
	receiveReq.open("GET", '../includes/getMessages.php?crid=' + chatRoomId + '&lm=' + lastMessage, true);
	receiveReq.onreadystatechange = function() {
		alert(receiveReq.readyState + " - " + receiveReq.status);
		if (receiveReq.readyState == 4 && receiveReq.status == 200) {
			var xmldoc = receiveReq.responseXML;
			var message_nodes = xmldoc.getElementsByTagName("message");
			var n_messages = message_nodes.length
			for (i = 0; i < n_messages; i++) {
				var user_node = message_nodes[i].getElementsByTagName("user");
				var text_node = message_nodes[i].getElementsByTagName("text");
				var time_node = message_nodes[i].getElementsByTagName("time");
				lastMessage = (message_nodes[i].getAttribute('id'));

				eDIV = document.createElement("div");
				eDIV.setAttribute("class","FullMessage");
				eAnchor = document.createElement("a");
				eAnchor.setAttribute("href","#");
				eAnchor.setAttribute("target","_blank");
				eAnchor.appendChild(document.createTextNode(user_node));
				eDIV.appendChild(eAnchor);
				eDIV.appendChild(document.createTextNode(" (" + time_node + "): " + text_node));
				obj.appendChild(eDIV);
				/*<div class="FullMessage"><a href="#" target="_blank">alyis</a> (9:25 AM): Hi everyone this is my first message in the chat room.</div>*/
			}
			mTimer = setTimeout('getMessages(' + chatRoomId + ',' + divId + ');',2000);
		}
	}
	receiveReq.send(null);
}
}

 

HTML:

<body onload="scrollBottom('ChatRoom'); getMessages('1','ChatRoom');">

 

Second HTML peice:

<div id="ChatRoom">
</div>

 

PHP:

<?php
require_once("functions.php");
require_once("objects.php");

$db = new Database("syck_polkamon");
$db->connectDB("syck","4<N#ly11TGn<");

//Send some headers to keep the user's browser from caching the response.
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: application/xml; charset=utf-8");

//Create the XML response.
$xml = '<?xml version="1.0" ?><root>';

if($_GET['lm']==0) {
    $sql = mysql_query("SELECT * FROM `chat_terminal` WHERE `chatid` = '$_GET[crid]' ORDER BY `timesent` DESC LIMIT 20");
    while($info = mysql_fetch_array($sql)) {
        $xml .= '<message id="' . $info['id'] . '">';
        $sql2 = mysql_query("SELECT * FROM `user_profiles` WHERE `id` = '$info[userid]'");
        $info2 = mysql_fetch_array($sql2);
        $xml .= '<user>' . htmlspecialchars($info2['username']) . '</user>';
        $xml .= '<text>' . htmlspecialchars($info['message']) . '</text>';
        $time = getDBDate('g:i A', $info['timesent']);
        $xml .= '<time>' . $time . '</time>';
        $xml .= '</message>';
    }
}else{
    $sql = mysql_query("SELECT * FROM `chat_terminal` WHERE `chatid` = '$_GET[crid]' AND `id` > '$_GET[lm]' ORDER BY `timesent` DESC");
    while($info = mysql_fetch_array($sql)) {
        $xml .= '<message id="' . $info['id'] . '">';
        $sql2 = mysql_query("SELECT * FROM `user_profiles` WHERE `id` = '$info[userid]'");
        $info2 = mysql_fetch_array($sql2);
        $xml .= '<user>' . htmlspecialchars($info2['username']) . '</user>';
        $xml .= '<text>' . htmlspecialchars($info['message']) . '</text>';
        $time = getDBDate('g:i A', $info['timesent']);
        $xml .= '<time>' . $time . '</time>';
        $xml .= '</message>';
    }
}

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

It displays:

[object HTMLCollection] ([object HTMLCollection]): [object HTMLCollection]

 

Thanks in advance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.