By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,303 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,303 IT Pros & Developers. It's quick & easy.

IE6 strange behaviour with fwrite calls

P: n/a
Hi devs,
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="radio" name="CheckMe" onClick='UpdateRecipeID(3);'/></
td>

The UpdateRecipeID() method launches an AJAX request.

function UpdateRecipeID(r_ID)
{
var xmlHttp = getXMLHttp();
var phpUrl;
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
phpUrl = "ajax.php?id=" + r_ID;

xmlHttp.open("GET", phpUrl, true);
xmlHttp.send(null);
}

function HandleResponse(response)
{
document.getElementById('ResponseDiv').innerHTML = response;
}

The php code is very simple: (ajax.php)

<?php
require_once("mylibrary.inc.php");
WriteTextFile($_REQUEST["id"],"bridge.txt");

echo "Selezionata ricetta: " .$_REQUEST["id"];
?>

(mylibrary.inc.php)
function WriteTextFile($stream, $fname)
{
$handle = fopen($fname, 'w');
if (!$handle ) {
echo "Cannot open file ($fname)";
exit;
}
fwrite($handle, $stream);

fclose($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?
Oct 29 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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="radio" name="CheckMe" onClick='UpdateRecipeID(3);'/></
td>

The UpdateRecipeID() method launches an AJAX request.

function UpdateRecipeID(r_ID)
{
var xmlHttp = getXMLHttp();
var phpUrl;
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
phpUrl = "ajax.php?id=" + 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 = "ajax.php?id=" + r_ID + "&amp;sid="+Math.random();

or a timestamp.
>
xmlHttp.open("GET", phpUrl, true);
xmlHttp.send(null);
}

function HandleResponse(response)
{
document.getElementById('ResponseDiv').innerHTML = response;
}

The php code is very simple: (ajax.php)

<?php
require_once("mylibrary.inc.php");
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?
>
echo "Selezionata ricetta: " .$_REQUEST["id"];
?>

(mylibrary.inc.php)
function WriteTextFile($stream, $fname)
{
$handle = fopen($fname, 'w');
if (!$handle ) {
echo "Cannot open file ($fname)";
exit;
}
fwrite($handle, $stream);

fclose($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
Oct 29 '08 #2

P: n/a
On Oct 29, 6:47*pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
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="radio" name="CheckMe" onClick='UpdateRecipeID(3);'/></
td>
The UpdateRecipeID() method launches an AJAX request.
function UpdateRecipeID(r_ID)
{
* var xmlHttp = getXMLHttp();
* var phpUrl;
* xmlHttp.onreadystatechange = function()
* {
* * if(xmlHttp.readyState == 4)
* * {
* * * HandleResponse(xmlHttp.responseText);
* * }
* }
* phpUrl = "ajax.php?id=" + 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 = "ajax.php?id=" + r_ID + "&amp;sid="+Math.random();

or a timestamp.
* xmlHttp.open("GET", phpUrl, true);
* xmlHttp.send(null);
}
function HandleResponse(response)
{
* document.getElementById('ResponseDiv').innerHTML = response;
}
The php code is very simple: (ajax.php)
<?php
* require_once("mylibrary.inc.php");
* 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?


* echo "Selezionata ricetta: " .$_REQUEST["id"];
?>
(mylibrary.inc.php)
function WriteTextFile($stream, $fname)
{
* *$handle = fopen($fname, 'w');
* *if (!$handle ) {
* * * * echo "Cannot open file ($fname)";
* * * * exit;
* *}
* *fwrite($handle, $stream);
* *fclose($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!
Oct 29 '08 #3

P: n/a
phpUrl = "ajax.php?id=" + r_ID + "&amp;sid="+Math.random();
or a timestamp.
Yep, the caching problem. ;-)
Erwin you are right! The problem was fixed adding the random sid. So
is this tecnique needed to fix IE caching problems?
Oct 30 '08 #4

P: n/a
CiAlZ schreef:
>phpUrl = "ajax.php?id=" + r_ID + "&amp;sid="+Math.random();
or a timestamp.
>Yep, the caching problem. ;-)

Erwin you are right! The problem was fixed adding the random sid. So
is this tecnique needed to fix IE caching problems?
Yes. But the problem is not limitted to IE only, so always use this trick.

Glad it works.

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
Oct 30 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.