Jump to content

chat box spazzing constantly


dyr

Recommended Posts

Hi, I recently commissioned a chatbox script and then recoded it using ajax to try and make the chat seem instant.  I'm trying to get it to work like a cbox, like the one here: http://cbox.ws/ (right hand column, except i'd like new messages to appear at the top)

 

However my script refreshes the messages instead of adding new ones.  It's distracting because it refreshes the old chat history and makes it hard to read, which leads me to think I coded it the wrong way.

 

chat.php

<?php
include('config.php');

if(isset($_POST['form']))
{
$message = mysql_real_escape_string($_POST['message']);
$time = time();
if(isset($_SESSION['id'])) { $userID = (int) $_SESSION['id']; }
else { $userID = 0; }
mysql_query("INSERT INTO `chat` (`userID`, `time`, `message`) VALUES ('$userID', '$time', '$message')") or die(mysql_error());
}

echo<<<JS
<script type="text/javascript">
function Ajax(){
var xmlHttp;
try{	
	xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
}catch (e){
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
	}catch (e){
	    try{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
			alert("No AJAX!?");
			return false;
		}
	}
}
xmlHttp.onreadystatechange=function(){
	document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
	setTimeout('Ajax()',30000);
}
xmlHttp.open("GET","chat_hist.php",true);
xmlHttp.send(null); 
}
window.onload=function(){
setTimeout('Ajax()',5000);
}
</script>
JS;

echo '<div id="ReloadThis">Loading...</div>';

?>

 

chat_hist.php

<?php

include('config.php');

$query = mysql_query("SELECT * FROM `chat` ORDER BY `id` DESC LIMIT 20");
while($row = mysql_fetch_assoc($query))
{
if($row['userID'] == 0) { $player = 'Guest'; }
else
{
	$player_q = mysql_query("SELECT * FROM `users` WHERE `id`='" . $_SESSION['id'] . "'");
	$player_r = mysql_fetch_assoc($player_q);
	$player = $player_r['callname'];
}
echo '<div style="border-bottom: 1px dashed black;">' . $player . ': ' . $row['message'] . '</div>
<div style="border-bottom: 1px solid black;">' . date('m-d h:i:s', $row['time']) . '</div>';
}

?>

Link to comment
Share on other sites

Thank you very much for assisting me!  So you recommend combining my chat_hist.php with the chat.php, and removing the ajax I currently have?  And then writing new Ajax to automatically add a new message in to the div (on another page where I include('chat.php'))?

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.