XMLHTTP in IE problems - please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skolar
Forum Newbie
Posts: 11
Joined: Mon May 09, 2005 12:36 pm

XMLHTTP in IE problems - please help

Post by skolar »

I've been trying to write this AJAX function for a couple days but i keep running into a problem that only occurs with IE. It works fine in firefox and safari.

What i'm trying to do is implement a simple tagging feature for articles on my site. A user types in a tag, its get recording in the db and gets outputted to the screen with all the other tags. What happens in IE is that first couple of times it works, but then it just stops working.

I linked the javascript function to a link ("<a href="javascript:function()">...</a>") but when i click on the link it all of a sudden doesnt work, it does nothing. This is after it working for the previous 2 or three times.

Also, when i type a new tag (tag1), click the link, it updates the list of tags. I type another new tag (tag2), click the link, it updates the list of tags. Now when type in tag1 again, click the link, tag2 dissappears from the list?!!?!

is there something i'm doing wrong? Do i need to do something with the activeXobject? I ran the code with MS script debugger, and it is returning an object on those "do nothing calls", however it is not performing the xmlhttp.open() function. I verified this by checking packets coming in and out. There were no calls to the serverside script when call the javascript function.

Here is my code:

Code: Select all

<script language="JavaScript" type="text/javascript">
//XMLhttp variable will hold the XMLHttpRequest object
function getHTTPObject() {
  	var xmlhttp;
	xmlhttp = false;        
	// If the user is using Mozilla/Firefox/Safari/etc
	if (window.XMLHttpRequest) {
	   //Intiate the object
  	   xmlhttp = new XMLHttpRequest();
   	   //Set the mime type
   	   //xmlhttp.overrideMimeType('text/html');
	}
	// If the user is using IE
	else if (window.ActiveXObject) {
		//Intiate the object
   		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
  return xmlhttp;
}

function preSearch() {
	var xmlhttp = getHTTPObject(); // We create the HTTP Object
    //Put the form data into a variable
	var theQuery = document.getElementById('tag_id').value;
	var job_id = document.getElementById('j').value;
    //If the form data is *not* blank, query the DB and return the results
	if(theQuery !== "") {
		var url = 'ajax.php?tag=' + theQuery + '&j=' + job_id;
		//document.write(url);
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		if(xmlhttp) {
		xmlhttp.open('GET', url, true);
		//Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			//document.getElementById('tag_results').innerHTML = xmlhttp.readyState;
			if((xmlhttp.readyState == 4)&&(xmlhttp.status == 200)) {
                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('tag_results').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				document.getElementById('tag_results').innerHTML = ' Tagging... ';
			}
		};
		xmlhttp.send(null);
		}
		else { document.getElementById('tag_results').innerHTML = 'xmlhttp is false'; }  
	}
}

function preSearchBool(e) {
	var pK = document.all? window.event.keyCode:e.which;
	return pK != 13;
}

document.onkeypress = preSearchBool;
</script>
Thanks in advance
skolar
Forum Newbie
Posts: 11
Joined: Mon May 09, 2005 12:36 pm

Post by skolar »

i found the cause and i thought i should share so no else pulls their hair out. I guess in IE the reponses are cached. So duplicate tags will return the "old" responses.

To mitigate this, you need to set the headers of your response to not cache.

This is what worked for me:

Code: Select all

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
This is for a server side php script that handles the xmlhttp request. You have to do something similar for java backend responses.

Hope this helps.
Post Reply