opalelement Posted June 13, 2009 Share Posted June 13, 2009 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? Quote Link to comment Share on other sites More sharing options...
jacksonmj Posted June 13, 2009 Share Posted June 13, 2009 The tutorial code seems to expect a "chat=[number]" in the URL when sending or receiving messages - so perhaps you should try something like: chat.php?action=chat&mode=get&last=0&chat=1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.