Jump to content

[SOLVED] Ajax Chat Help


Zhadus

Recommended Posts

I'm trying to develop a very small scale and simple chatroom for 2-3 people to use at a time. It will be used for admins only on a website.

 

To start with, I'm just having all the chat text stored in a text file and there is no login or verification etc. it just posts using IP.

 

It seems to work properly on IE6, but it wipes the 'chat' window whenever it refreshes on IE7.

 

Does anyone see the issue? Any comments on simplifying it or a better way of doing things will be appreciated.

 

easyim.html

<html>
<head>
<script type="text/javascript">
function refreshfunc(num)
{
ajaxRequest("read");
setTimeout('refreshfunc(5)', num * 1000);
}

function callback(serverData, serverStatus, mainval) { // Called automatically when we get data back from server
if (mainval == 'read')
document.getElementById('output').innerHTML = serverData; // Display an alert box with the recieved data
else
ajaxRequest('read');
}

function ajaxRequest(mainval) {
var url;
if (mainval == 'read')
url='MYPATH/getdata.php?doc=chat.txt'; // Get File URL
if (mainval == 'write')
url='MYPATH/writedata.php?doc=chat.txt&message=' + mainMSG; // Write File URL
var mainMSG = document.getElementById('message').value;
if (mainval == 'write')
document.getElementById('message').value = '';
var AJAX = null; // Initialize the AJAX variable.
if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
AJAX=new XMLHttpRequest(); // Yes -- initialize it.
} else { // No, try to initialize it IE style
AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
} // End setup Ajax.
if (AJAX==null) { // If we couldn't initialize Ajax...
alert("Your browser doesn't support AJAX."); // Sorry msg. 
return false // Return false, couldn't set up ajax
}
AJAX.onreadystatechange = function() { // When the browser has the request info..
if (AJAX.readyState==4 || AJAX.readyState=="complete") { // see if the complete flag is set.
callback(AJAX.responseText, AJAX.status, mainval); // Pass the response to our processing function
} // End Ajax readystate check.
}
AJAX.open("GET", url, true); // Open the url this object was set-up with.
AJAX.send(null); // Send the request.
}
</script>
</head>
<body onload="refreshfunc(5);">
<input type="button" value="Refresh" onclick="ajaxRequest('read');">
<input type="text" id="message">
<input type="button" value="Send Message" onclick="ajaxRequest('write');">
<div id="output"></div>
</body>
</html>

 

getdata.php

<?php
$myFile = $_GET['doc'];
$fh = fopen($myFile, 'r');
$old = '';
while(!feof($fh)) 
$old .= fgets($fh);
echo $old;
?>

 

writedata.php

<?php
date_default_timezone_set('GMT');
$myFile = $_GET['doc'];
$chat = stripslashes($_GET['message']);
$fh = fopen($myFile, 'r');
$old = '';
while(!feof($fh)) 
$old .= fgets($fh);
fclose($fh);

$fh = fopen($myFile, 'w');
$ip = $_SERVER['REMOTE_ADDR']; 
$time = date('h:i:s');
$chat = '(' . $time . ') ' . $ip . ': ' . $chat . '<br />' . $old; 
if (fwrite($fh, $chat) === FALSE)
{
exit;
}
?>

Link to comment
Share on other sites

Now that I figured out the problem. Fully agreed. Without internet browser settings set to update page each visit, it doesn't work. Friend was set on "automatic" and only worked when closing browser.

 

Anyway, a conversion to database from this should be easy and a bit more efficient.

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.