xmlhttprequest 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

xmlhttprequest help!!!

Post by skolar »

I'm using xmlhttprequest to grab info from my database and output it to the screen without having to refresh the page...a fairly standard and seemingly easy task for AJAX.

However, i always seem to be getting a null response in the xmlhttprequest object. I verified its null two ways, with xmlhttprequest debugger and by checking the actual packets with ethereal.

When i call the server script, with the same request url as passed to the xmlhttprequest object, manually, everything works fine. Its only when i call it through the object that the response is null. Does anyone have any idea whats going on?

Also, when checking ethereal, the http GET returns 200 but the data is not to be found. What happened to the output?

I'm new to AJAX and from what i've seen online, server side php scripts are just echo'ing the data to be returned to the object. Is this correct? Do i need to do something else to return the data?

Thanks in advance
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

As you have no idea on what's going on, it would be useful to see your code (javascript part would be sufficient to start with).
skolar
Forum Newbie
Posts: 11
Joined: Mon May 09, 2005 12:36 pm

Post by skolar »

Code: Select all

<script language="JavaScript">
//XMLhttp variable will hold the XMLHttpRequest object

function preSearch() {
	var 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("Microsoft.XMLHTTP");
	}
    //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 = 'http://www.karmaone.org/staging/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 = ' ' + xmlhttp.status + ' ' + xmlhttp.readyState;
			}
		};
		xmlhttp.send(null);
		}
		else { document.getElementById('tag_results').innerHTML = 'xmlhttp is false'; }  
	}
}
</script>
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Have you looked at Burrito's tutorial about whos online... That may give you some hints.
Post Reply