PDA

View Full Version : JS+PHP chat room, need help.



mechakoopa
10-08-2005, 05:09 AM
I am devloping non-refresh chat room using JS and PHP with XMLHTTPRequest

Installation SQL:
CREATE TABLE chat1 (id INT(12) NOT NULL AUTO_INCREMENT, myname TEXT NOT NULL, chattext TEXT NOT NULL, PRIMARY KEY (id));

dbconfig.php - You know what will it contain, DB username, name and password

getchat.php - To get the room chat text.. From the current message ID

<?
include('./dbconfig.php');
$msq = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$numrow = mysql_num_rows(mysql_query("select id from chat1"));
$thestart = $numrow - $_GET['pars'];
echo "$numrow";
$eachq = mysql_query("select myname,chattext from chat1 order by id desc limit 0, $thestart");
while ($eaches = mysql_fetch_array($eachq)) {
echo ">" . htmlspecialchars($eaches[0]);
echo ">" . htmlspecialchars($eaches[1]);
}
mysql_close($msq);
?>

init.php - To tell the main script to get number of message since char started, so it won't show older message.

<?
include('./dbconfig.php');
$msq = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$numrow = mysql_num_rows(mysql_query("select id from chat1"));
$thestart = $numrow - $_GET['pars'];
echo "$numrow";
mysql_close($msq);
?>

post.php

<?
include('./dbconfig.php');
$msq = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$chatname = $_POST['name'];
$chattext = $_POST['text'];
if (trim($chatname) != "") {
if (trim($chattext) != "") {
$msq = mysql_connect("localhost", "root", "123");
mysql_select_db("xchatroom");
mysql_query("insert into chat1 (myname,chattext) values ('$chatname','$chattext')");
mysql_close($msq);
}
}
?>

Last one, index.php

<!--
##################################################
######## MechakoopaChat by Mechakoopa Revolution #########
## Download: http://mechakoopa.servegame.org/xchat/xchat.zip ##
###############################################################
## This is a XMLHttpRequest-based chat room. ##
## Use XMLHttpRequest() object that IE don't ##
## have. This program won't work with IE. ##
##################################################
## This program is free to use, but you must ##
## include this credit. DON'T REMOVE IT!!! ##
##################################################

-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My Chat Room</title>
</head>
<body>
<!--[Powered by XChat by Mechakoopa Revolution]-->
<div style="position:fixed; background-color:#CCCCCC; width:98%; border-style:ridge; border-width:5px; border-color:#CCCCCC">
<br><center>
<form method="post" action="post.php" target="pmt"
onSubmit="document.getElementById('reallymytext').value=document.getElementById('mytext').value;
document.getElementById('mytext').value = '';">
Name: <input type="text" name="name" size="20">
Text: <input type="text" size="60" id="mytext">
<input type="hidden" name="text" size="60" id="reallymytext">
<input type="submit" value="Submit" id="mesubmit">
</form>
</center><br>
</div>
<iframe name="pmt" style="display:none" width="1" height="1"></iframe>
<br><br><br><br><br>
<span id="displayspan"></span>


<script>
http = new XMLHttpRequest();
var currentpos = 0
function handleHttpResponse() {
if (http.readyState == 4) {
// Split the comma delimited response into an array
results = http.responseText.split(">");
currentpos = results[0];
for (i = 1; i < results.length; i++) {
if ((i%2) == 1) {
document.getElementById('displayspan').innerHTML += results[i] + " : ";
} else {
document.getElementById('displayspan').innerHTML += results[i] + "<br>";
}
}
setTimeout("gatherch();", 2000);
}
//document.getElementById('displayspan').innerHTML += "handl";
}

function gatherch() {
http.open("GET", "getchat.php?pars=" + currentpos, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
//document.getElementById('displayspan').innerHTML += "getch";
}


function initchat2() {
if (http.readyState == 4) {
currentpos = http.responseText;
setTimeout("gatherch();", 2000);
//document.getElementById('displayspan').innerHTML += "init2";
}
}

function initchat1() {
http.open("GET", "init.php", true);
http.onreadystatechange = initchat2;
http.send(null);
//document.getElementById('displayspan').innerHTML += "init1";
}

function scrolldn() {
window.scrollBy(0,1);
setTimeout("scrolldn()",5);
}
setTimeout("scrolldn()",5);
//document.getElementById('displayspan').innerHTML += "prepare";
initchat1();
</script>
</body>
</html>

It worked well on Firefox, but somehow not work on IE.

blm126
10-08-2005, 03:30 PM
If you are trying to get it work in IE you have a simple problem. IE does not support the below code.


http = new XMLHttpRequest();
For IE you have to use

http = new ActiveXObject("Microsoft.XMLHTTP")
So you should update that section of the code to read

try
{
http = new XMLHttpRequest();
}
catch (error)
{
try
{
http = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (error)
{
http = null;

return false;
}
}
This should solve your problem with IE.