Jump to content

AJAX chat script - not returning values?


opalelement

Recommended Posts

I am trying to get this chat script to work (from a tutorial here)

 

My current code:

<?php
if(!isset($_GET['mode']))
{
echo <<<REGION
<script type="text/javascript">
var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;

function getChatText() {
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
	receiveReq.open("GET", 'chat.php?action=chat&mode=get&last=' + lastMessage, true);
	receiveReq.onreadystatechange = handleReceiveChat; 
	receiveReq.send(null);
}			
}

function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
	return new XMLHttpRequest();
} else if(window.ActiveXObject) {
	return new ActiveXObject("Microsoft.XMLHTTP");
} else {
	document.getElementById('chat_status').innerHTML = 
	'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.';
}
}

function handleReceiveChat() {
if (receiveReq.readyState == 4) {
	var chat_div = document.getElementById('div_chat');
	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");
		chat_div.innerHTML += user_node[0].firstChild.nodeValue + ' ';
		chat_div.innerHTML += '<font class="chat_time">' 
		chat_div.innerHTML += time_node[0].firstChild.nodeValue + '</font><br />';
		chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
		lastMessage = (message_nodes[i].getAttribute('id'));
	}
	mTimer = setTimeout('getChatText();',2000);
}
}

function sendChatText() {
if (sendReq.readyState == 4 || sendReq.readyState == 0) {
	sendReq.open("POST", 'chat.php?action=chat&mode=send', true);
	sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	sendReq.onreadystatechange = handleSendChat; 
	var param = 'message=' + document.getElementById('txt_message').value;
	//param += '&name=Ryan Smith';
	//param += '&chat=1';
	sendReq.send(param);
	document.getElementById('txt_message').value = '';
}							
}

function handleSendChat() {
//Clear out the existing timer so we don't have 
//multiple timer instances running.
clearInterval(mTimer);
getChatText();
}
</script>

	<h2>AJAX Driven Web Chat.</h2>
	<p id="p_status">Status: Normal</p>
	Current Chitter-Chatter:
	<div id="div_chat" class="chat_main" style="border:solid 1px;background-color: #DDCCAA; margin-left: 2px;padding:3px;">

	</div>
	<form id="frmmain" name="frmmain" onsubmit="">
		<input type="button" name="btn_get_chat" id="btn_get_chat" value="Refresh Chat" onclick="javascript:getChatText();" />
		<input type="button" name="btn_reset_chat" id="btn_reset_chat" value="Reset Chat" onclick="javascript:resetChat();" /><br />
		<input type="text" id="txt_message" name="txt_message" style="width: 447px;" />
		<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText();" />
	</form>
REGION;
}
else
{
if($_GET['mode'] == "get")
{
	$header_auth = true;
	include("validate.php"); // Validates the login
	echo <<<REGION
	<?xml version="1.0" ?><root>
	<message id="0">
	<user>Admin</user>
	<text>Welcome</text>
	<time>12:00</time>
	</message>
	</root>
REGION;

}
else if($_GET['mode'] == "send")
{

}
}

?>

 

The output of directly accessing the chat.php?action=chat&mode=get&last=0:

		<root> 
	<message id="0"> 
	<user>Admin</user> 
	<text>Your are not currently in a chat session.</text> 
	<time>7:30</time> 
	</message> 
	</root>

 

I don't understand why it is not returning the chat... Can someone help me?

 

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.