|
Posted by Carlo Chiari on October 29, 2008, 3:36 pm
Please log in for more thread options
On Oct 29, 6:47=A0pm, Erwin Moller
> Carlo Chiari schreef:
>
> > Hi devs,
>
> Hi Carlo,
>
>
>
> > in my application I need to update a simple text file every time the
> > user checks an item of a radio group.
> > I use AJAX method to do this stuff.
>
> > In the start page I have this code for evely row:
>
> > <td><input type=3D"radio" name=3D"CheckMe" onClick=3D'UpdateRecipeID(3)=
;'/></
> > td>
>
> > The UpdateRecipeID() method launches an AJAX request.
>
> > function UpdateRecipeID(r_ID)
> > {
> > =A0 var xmlHttp =3D getXMLHttp();
> > =A0 var phpUrl;
> > =A0 xmlHttp.onreadystatechange =3D function()
> > =A0 {
> > =A0 =A0 if(xmlHttp.readyState =3D=3D 4)
> > =A0 =A0 {
> > =A0 =A0 =A0 HandleResponse(xmlHttp.responseText);
> > =A0 =A0 }
> > =A0 }
> > =A0 phpUrl =3D "ajax.php?id=3D" + r_ID;
>
> That is bad.
> You will get a cached response back in most circumstances.
> Add something random to it to avoid caching:
> eg:
> phpUrl =3D "ajax.php?id=3D" + r_ID + "&sid=3D"+Math.random();
>
> or a timestamp.
>
>
>
> > =A0 xmlHttp.open("GET", phpUrl, true);
> > =A0 xmlHttp.send(null);
> > }
>
> > function HandleResponse(response)
> > {
> > =A0 document.getElementById('ResponseDiv').innerHTML =3D response;
> > }
>
> > The php code is very simple: (ajax.php)
>
> > <?php
> > =A0 require_once("mylibrary.inc.php");
> > =A0 WriteTextFile($_REQUEST["id"],"bridge.txt");
>
> Using $_REQUEST is bad in my opinion. It only shows you don't know where
> the data comes from.
> Why not simply use $_GET instead?
>
>
>
>
>
> > =A0 echo "Selezionata ricetta: " .$_REQUEST["id"];
> > ?>
>
> > (mylibrary.inc.php)
> > function WriteTextFile($stream, $fname)
> > {
> > =A0 =A0$handle =3D fopen($fname, 'w');
> > =A0 =A0if (!$handle ) {
> > =A0 =A0 =A0 =A0 echo "Cannot open file ($fname)";
> > =A0 =A0 =A0 =A0 exit;
> > =A0 =A0}
> > =A0 =A0fwrite($handle, $stream);
>
> > =A0 =A0fclose($handle);
> > }
>
> > This source code works fine with Safari 3 and Chrome too, but with IE6
> > works only for the first three times I click a row.
>
> > Anyone knows where's the problem?
>
> Yep, the caching problem. ;-)
>
> Good luck fixing it. (Just add the Math.random() thingy to it)
>
> Regards,
> Erwin Moller
>
> --
> "There are two ways of constructing a software design: One way is to
> make it so simple that there are obviously no deficiencies, and the
> other way is to make it so complicated that there are no obvious
> deficiencies. The first method is far more difficult."
> -- C.A.R. Hoare
Thanx Erwin, tomorrow I'll try your precious suggestions. I'm slightly
new to AJAX world!
|