...

View Full Version : xmlhttp doesn't update



RaptorIV
07-27-2009, 09:07 PM
I'm using an xmlhttp request to get information from a database. Everything works fine in Firefox, and it works in IE until you try to refresh the page. If a news post (thats what it is getting from the database) is deleted it will continue to show the same information.

I can't really post all of the script because it (along with the html and php) are spread out through many different pages:



//JS
var xmlhttp;

function refreshNews()
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="library/admin_processnews.php?access=1&show_news=1";
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function deleteNews(id)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="library/admin_processnews.php?access=1&show_news=1&do=delete_news&delete="+id;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
alert(xmlhttp.responseText);
document.getElementById("news").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}




//PHP
<?
require("dbconnect.php");

if($_GET['access']==1){
switch($_GET['do']){
case 'post_news':
@mysqli_query($con, 'INSERT INTO news (title, date, content) VALUES ("'.$_GET['title'].'", "'.date('m/d/Y').'", "'.$_GET['content'].'")') or die("Couldn't post announcement: ".mysqli_error());
break;
case 'delete_news':
@mysqli_query($con, "DELETE FROM news WHERE id=".$_GET['delete']) or die("Couldn't delete Announcement: ".mysqli_error());
$query = mysqli_query($con, "SELECT * FROM news ORDER BY id Desc") or die(mysql_error());
break;
default:
}
}

if($_GET['show_news']==1){
$query = mysqli_query($con, "SELECT * FROM news ORDER BY id Desc") or die(mysql_error());

while ($row = mysqli_fetch_assoc($query)) {
if($_GET['access']==1){
echo "<h1>".$row['title']." <a href='#' onClick=deleteNews(".$row['id'].")>(Delete)</a></h1>";
}else{
echo "<h1>".$row['title']."</h1>";
}
print "<h2>".$row['date']."</h2>";
echo "<p>".$row['content']."</p>";
}
}
?>

refreshNews() is called in the onLoad event of the body in the html.

RaptorIV
07-27-2009, 10:07 PM
Please help :confused:

12 Pack Mack
07-27-2009, 10:21 PM
RaptorIV:

It might help if you included the "please" in the initial post.


xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
xmlhttp.send(null);

RaptorIV
07-27-2009, 10:26 PM
haha, I'll remember that next time. Now it just gives me an "Unspecified Error" in IE. Works in firefox still though.

12 Pack Mack
07-27-2009, 10:33 PM
haha? I'm not amused.

http://codingforums.com/showthread.php?p=787825&highlight=cache#post787825
http://codingforums.com/showthread.php?t=142180&highlight=SetRequestHeader

RaptorIV
07-27-2009, 10:40 PM
Adding a setRequestHeader stops it from working on firefox and causes an error on IE. I added header information to the php page but that didn't do anything.
Still have that same problem :(

12 Pack Mack
07-27-2009, 10:43 PM
var forceGET = "?n="+ parseInt(Math.random()*999999999);
cityRequest.open("GET", "WeatherFiles/Cities.xml"+forceGET, true);
cityRequest.send(null);

RaptorIV
07-27-2009, 10:46 PM
Thank you!

12 Pack Mack
07-27-2009, 11:02 PM
My initial post had the setRequestHeader out of order.

You really should use that and not a random number.

I corrected my initial post.

The setRequestHeader comes after "open".

NorbNYC
08-18-2012, 04:37 AM
Hi all, sorry to pull up what appears to be an ancient thread, but I'm am having the same caching issue using IE. I'm not clear as how to remediate it based on the above (sorry, somewhat new to JS).
Where should setRequestHeader come in my case below (somewhat elementary I know):




var client = new XMLHttpRequest();
client.open('GET', 'http://localhost:8080/file.txt');
client.send();
var inputtxt = client.responseText;


Thanks in advance



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum