Forum Moderators: open

Message Too Old, No Replies

Ajax Chat

         

qblue

2:59 pm on Jun 21, 2006 (gmt 0)



Hello!

I'm started create simple Chat with Ajax!

I'm finish basic chat, but they have a problem.
When i activate my function timer from <body onload="getDataFromServer()"> then always a got old XML from server. Under this i think i got XML with data witch was there when i'm inicialize me chat page, although in meantime i set new data to database.

When i send new data to server, server insert this data to database and return me again "old" XML.

If i turn off timer function, then everything is ok, but always i must click send button to get data from server, and this is not dinamic.

And i'm now lost!

In this moment chat has two files, index.php and getdata.php.

Files content:

index.php
=============================================

<html>
<head>
<title> Qisland CHAT </title>
</head>

<script type="text/javascript">
function provjera(){
var ime=document.getElementById("nick").value.length;
if (ime>0) {
document.getElementById("forma1").submit();
}
else {
document.getElementById("nick").focus();
}
}

// KOD ZA AJAX

// ** kreiranje XMLHTTP objekta preko kojeg se vrši promet Ajax-om
function kreirajObjekt(){
var req;

if (window.XMLHttpRequest)
req = new XMLHttpRequest();
else if (window.ActiveXObject)
req = new ActiveXObject("Microsoft.XMLHTTP");
else
alert ("Greška kod kreiranja objekta za Ajax!");

return req;
}

// kreiranje varijable koja če predstavljat objekt XMLHttpRequest

var http = kreirajObjekt();

// kreiranje funkcije za slanje podataka serveru
// tip=1 ako se šalju podaci u bazu ili tip=0 ako se samo traže podaci iz baze

function slanjeZahtjeva(tip){

if (tip==1)
http.open('get', "getdata.php?speekarea="+document.getElementById(" speekarea").value+"&nickname="+
document.getElementById("nickname").value+"&tip="+ tip, true);
else
http.open('get', "getdata.php?speekarea=0&nickname=0$tip="+tip, true);

http.onreadystatechange = upravljanjeZahtjevom;
http.send(null);
}

// kreiranje funkcije koja če upravljati primljenim podacima od servera

function upravljanjeZahtjevom(){
if (http.readyState==4 && http.status == 200){
var pristiglo = http.responseText;
if (pristiglo)
document.getElementById("chatarea").value = pristiglo;
}
}

//////////////////////////////
//////// Critical part
/////////////////////////////

// TIMER koji če periodički skidat podatke sa servera

function getDataFromServer(){
slanjeZahtjeva('0');

setTimeout('getDataFromServer()', 500);
}

////////////////////////////////
////////////////////////////////
////////////////////////////////

</script>

<?php

$ime = $_POST["nick"];

if (isset($ime)){
echo "
<body bgcolor=\"#6060cc\" onload=\"getDataFromServer()\">>
<h1><center><font color=\"#ddaaaa\"> Wellcom Qisland Chat!</font></center></h1><br><br>

<form>Nick: " . $ime . "<br>
<a href=\"http://tutorials.awardspace.com/Chat/index.php\"> Back </a><br><hr>
<input type=\"hidden\" name=\"nickname\" value=\"" . $ime . "\">
<textarea rows=\"25\" cols=\"100\" id=\"chatarea\" name=\"chatarea\" readOnly=\"true\"></textarea><br>
<input type=\"text\" size=\"120\" id=\"speekarea\" name=\"speekarea\">
<input type=\"button\" id=\"senddata\" name=\"senddata\" value=\"Send\" onClick=\"slanjeZahtjeva('1')\">

</form>";

}
else{
echo "
<body bgcolor=\"#6060cc\">
<h1><center><font color=\"#ddaaaa\"> Wellcom Qisland Chat!</font></center></h1><br><br>

<form action=\"index.php\" method=\"POST\" id=\"forma1\">
Enter Nick: <input type=\"text\" id=\"nick\" name=\"nick\" maxLength=\"20\" size=\"22\">
<input type=\"button\" id=\"submit_form\" value=\"Uđi na chat\" onClick=\"provjera()\">";
}

echo "
</form>
</body>
</html>";

?>

getdata.php
================================================

<?php

$var1 = $_GET["nickname"];
$var2 = $_GET["speekarea"];

if ($_GET["tip"]){

$var3 = date("H:i");
$var4 = date("Y-n-j");

$baza = mysql_connect('#*$!', 'xxx', 'xxx') or die ('Server sa bazom nije dostupan');
mysql_select_db('xxx') or die ('Greska kod spajanja na bazu podataka');

$rez = mysql_query("insert into chat values('$var1', '$var2', '$var3', '$var4')") or die ("Greska kod upisa u bazu (kat već postoji)");
mysql_close($baza);
}

$baza = mysql_connect('xxx', 'xxx', 'xxx') or die ('Server sa bazom nije dostupan');
mysql_select_db('xxx') or die ('Greska kod spajanja na bazu podataka');

$rez = mysql_query('select * from chat');
$redovi = mysql_num_rows($rez);

for ($i=0; $i<$redovi; $i++){

$nick = mysql_result($rez, $i, 0);
$red = mysql_result($rez, $i, 1);

echo $nick . ": " . $red . "\n";
}
mysql_close($baza);

?>

So if anybody see or know where i'm wrong...
I hope somebody know what is wrong whit this.
Thanks!

Bye!

adni18

6:34 pm on Jun 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know the first thing about SQL databases, but it sounds like you are getting a cached page. Try accessing the php file with a random numeric argument, like index.php?4950389450394 to ensure that you're not getting a cached page.

jshanman

6:49 pm on Jun 23, 2006 (gmt 0)

10+ Year Member



it sounds like you are getting a cached page.

I agree. As a standard, I always use the valueOf() method of a new date object as the last argument for any server call...

var d = new Date();
var url = "page.php?arg1=bla&d="+d.valueOf();

- JS

FidoDldo

8:29 am on Jun 30, 2006 (gmt 0)



This is indeed a caching problem.

Using the POST method in stead of the GET method with ajax doesn't have this problem ...

Using the GET method isn't a good idea any way! every one can see what data is communicated between the client and the server (passwords included!)