the other day I was searching for a examples and I came across a old thread I started trying to make a chat script..
The thread was closed so I couldn't ask My question there..
in the thread jpadie shared a script with me..
ChatRoomServer.php
ChatRoomServer.js
index.html
Now I did change the function chats
from
to
and it is connecting and passing variables to the Database but the chat is not returning the results to the page?.. what have I missed?
I should mention I am using xamp as a local host..
MA WarGod
I believe if someone can think it, it can be programmed
The thread was closed so I couldn't ask My question there..
in the thread jpadie shared a script with me..
ChatRoomServer.php
Code:
<?php
$chat = new chats;
class chats{
private $timestamp = 0;
private $action;
function chats(){
$this->timestamp = time();
$dbserver = mysql_connect('localhost', 'mychat', 'mychat') or die('error');
@mysql_select_db('mychat',$dbserver) or die('error');
$result = mysql_query("SHOW TABLES LIKE 'chat'");
if (mysql_num_rows($result) === 0){
$this->createDatabaseSchema();
}
$this->action = isset($_POST['action']) ? $_POST['action'] : 'getChatData';
$this->switchBoard();
}
function switchBoard(){
switch ($this->action){
case 'writeChatData':
if ($this->writeChatData()){
echo '1';
} else {
echo '0';
; }
break;
default:
echo $this->getChatData();
}
}
function getChatData(){
$return ='';
$timestamp = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
if ($timestamp == 'null') $timestamp = 0;
$query = "Select chatID, chatPerson, chatTimestamp, chatText from chat where chatTimestamp > ? order by chatTimestamp DESC limit 50 ";
$stmnt = $this->pdo->prepare($query);
$stmnt->execute(array($timestamp));
$chats = $stmnt->fetchAll();
foreach ($chats as $chat){
$date = date('Y-m-d H:i:s', $chat['chatTimestamp']);
$chatText = nl2br($chat['chatText']);
$return .= <<<HTML
<tr>
<td id="{$chat['chatID']}">{$chat['chatPerson']}<br/>$date</td>
<td>{$chatText}</td>
</tr>
HTML;
}
if (empty($return)){
return 'none';
} else {
return json_encode(array('timestamp'=>$this->timestamp,'chatText'=>$return));
}
}
function writeChatData(){
$chatPerson = empty($_POST['chatPerson']) ? NULL : trim($_POST['chatPerson']);
$chatText = empty($_POST['chatText']) ? NULL : trim($_POST['chatText']);
if (empty($chatText) || empty($chatPerson)){
return false;
} else {
if (get_magic_quotes_gpc()){
$chatPerson = stripslashes($chatPerson);
$chatText = stripslashes($chatText);
}
$query = "Insert into chat (chatTimestamp, chatPerson, chatText) values (?,?,?)";
$stmnt = $this->pdo->prepare($query);
if ($stmnt){
$stmnt->execute(array(time(), $chatPerson, $chatText));
} else {
print_r($this->pdo->errorInfo());
}
if ($stmnt !== false){
return true;
} else {
return false;
}
}
}
function createDatabaseSchema(){
$query = " create table
chat
(
chatID integer NOT NULL PRIMARY KEY AUTOINCREMENT,
chatTimestamp integer NOT NULL,
chatPerson text NOT NULL,
chatText varchar NOT NULL
)";
$this->pdo->exec($query);
}
}
?>
Code:
var timer = null;
var chatTable;
var timestamp = null;
var xo = null;
var url = 'chatRoomServer.php';
function createObject(){
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
xo = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xo = new XMLHttpRequest();
}
}
function updateChatRoom(){
if (!xo) {createObject();}
xo.onreadystatechange = function () {
if (xo.readyState == 4) {
if (xo.status == 200) {
if(xo.responseText == 'none'){
//do nothing
} else {
var rObj = eval('(' + xo.responseText + ')');
timestamp = rObj.timestamp;
//console.log('timestamp is '+ timestamp);
displayUpdatedChats(rObj.chatText)
}
}
}
};
try {
xo.open("POST",url, true);
} catch (e) {
console.log('problem in open command') ;
}
try {
xo.setRequestHeader("Content-type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
xo.send('action=getChatData×tamp='+timestamp);
} catch (E) {
console.log('problem retrieving new chats');
}
}
function displayUpdatedChats(txt){
chatTable.innerHTML = txt + chatTable.innerHTML;
}
function sendChat(){
var cP = document.getElementById('chatPerson');
var cT = document.getElementById('chatText');
var chatPerson = cP.value;
var chatText = cT.value;
var data = "action=writeChatData&chatPerson="+chatPerson+"&chatText="+chatText;
if (!xo) {createObject();}
xo.onreadystatechange = function () {
if (xo.readyState == 4) {
if (xo.status == 200) {
if(xo.responseText == '0'){
console.log ('problem saving chat data');
} else {
//cP.value = '';
cT.value = '';
}
}
}
};
try {
xo.open("POST", url, true);
} catch (e) {
console.log('problem in open command') ;
}
try {
xo.setRequestHeader("Content-type", "application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]
xo.send(data);
} catch (E) {
console.log('problem sending data');
}
return false;
}
window.onload = function(){
chatTable = document.getElementById('chatTable');
timer = setInterval(updateChatRoom, 5000);
};
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="chatRoomServer.js"></script>
<title>Chat Room</title>
</head>
<body>
<div id="chatForm">
Your Name: <input type="text" name="chatPerson" id="chatPerson"/><br/>
Message: <textarea name="chatText" id="chatText"></textarea><br/>
<input type="button" name="submit" value="Chat" onclick="sendChat();"/>
</div>
<div id="chatDiv">
<table id="chatTable" border="1">
</table>
</div>
</body>
</html>
Now I did change the function chats
from
Code:
function chats(){
require 'databaseConfig.txt';
$this->timestamp = time();
mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die (mysql_error());
mysql_select_db(DATABASENAME);
$result = mysql_query("SHOW TABLES LIKE 'chat'");
if (mysql_num_rows($result) === 0){
$this->createDatabaseSchema();
}
$this->action = isset($_POST['action']) ? $_POST['action'] : 'getChatData';
$this->switchBoard();
}
Code:
function chats(){
$this->timestamp = time();
$dbserver = mysql_connect('localhost', 'mychat', 'mychat') or die('error');
@mysql_select_db('mychat',$dbserver) or die('error');
$result = mysql_query("SHOW TABLES LIKE 'chat'");
if (mysql_num_rows($result) === 0){
$this->createDatabaseSchema();
}
$this->action = isset($_POST['action']) ? $_POST['action'] : 'getChatData';
$this->switchBoard();
}
I should mention I am using xamp as a local host..
MA WarGod
I believe if someone can think it, it can be programmed