getChat.php
<?php // wysylam naglowki require('inc/database.php'); // sprawdzam, czy wiadomosc zostala wyswietlona $sql = "INSERT INTO message(chat_id, user_id, user_name, message, akceptacja, post_time) VALUES (" . db_input($_GET['chat']) . ", 1, '" . db_input($_POST['name']) . "', '" . db_input($_POST['message']) . "', 0, NOW())"; db_query($sql); } // generuje XML $xml = '<?xml version="1.0" ?><root>'; //Check to ensure the user is in a chat room. $xml .= '<message id="0">'; $xml .= '<user>Admin</user>'; $xml .= '<text>:)</text>'; $xml .= '</message>'; } else { $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 . " AND akceptacja = 1"; $message_query = db_query($sql); while($message_array = db_fetch_array($message_query)) { $xml .= '<message id="' . $message_array['message_id'] . '">'; $xml .= '<time>' . $message_array['post_time'] . '</time>'; $xml .= '</message>'; } } $xml .= '</root>'; ?>
i plik chat.php odpowiedzialny za obróbkę danych z bazy i wyświetlenie wyników na stronie:
<script language="JavaScript" type="text/javascript"> var sendReq = getXmlHttpRequestObject(); var receiveReq = getXmlHttpRequestObject(); var lastMessage = 0; var mTimer; // inicjacja strony function startChat() { // aktywny kursor w miescu pisania document.getElementById('txt_message').focus(); // pisanie pytania getChatText(); } // wysylanie do przegladarki XmlHttpRequest Object function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.'; } } // wysylanie pytania function getChatText() { if (receiveReq.readyState == 4 || receiveReq.readyState == 0) { receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true); receiveReq.onreadystatechange = handleReceiveChat; receiveReq.send(null); } } // dodawanie pytania function sendChatText() { if(document.getElementById('txt_message').value == '') { alert("Proszę wpisać pytanie!"); return; } if (sendReq.readyState == 4 || sendReq.readyState == 0) { sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true); sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sendReq.onreadystatechange = handleSendChat; var param = 'message=' + document.getElementById('txt_message').value; param += '&name=<?php echo $_GET["name"] ?>'; param += '&chat=1'; sendReq.send(param); document.getElementById('txt_message').value = ''; } } // "odswiezanie" strony po wyslaniu pytania function handleSendChat() { clearInterval(mTimer); getChatText(); } //Function for handling the return of chat text 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"); // pokazuje godzine chat_div.innerHTML += '<font class="chat_time">' + time_node[0].firstChild.nodeValue + '</font> '; // pokazuje nick // pokazuje pytanie chat_div.scrollTop = chat_div.scrollHeight; lastMessage = (message_nodes[i].getAttribute('id')); } // odswieza co 2 sekundy mTimer = setTimeout('getChatText();',2000); } } //This functions handles when the user presses enter. Instead of submitting the form, we //send a new message to the server and return false. function blockSubmit() { sendChatText(); return false; } //This function handles the response after the page has been refreshed. function handleResetChat() { document.getElementById('div_chat').innerHTML = ''; getChatText(); } </script> </HEAD> <BODY onload="java script:startChat();"> <form id="frmmain" name="frmmain" onsubmit="return blockSubmit();"> <input type="text" id="txt_message" name="txt_message" style="width: 442px;" /> <input type="button" name="btn_send_chat" id="btn_send_chat" value="Wyślij" onclick="java script:sendChatText();" /> <BR> <input type="button" name="btn_get_chat" id="btn_get_chat" value="Odśwież czat" onclick="java script:getChatText();" /> </form>
Pytanie - jak wyświetlać na bieżąco ilość logowanych użytkowników oraz ich listę?