Hi,

I'm kind'a new to AJAX, and have not a lot experience with JS, so this might be just a stupid beginners-error...

I'm busy creating a PHP-website in which i'd like to implement some dynamic features (like adding comments tot photo's without having to reload, searching a user-DB, etc). I've worked myself trough the main part of the AJAX for Dummies which gives clear examples of AJAX communication with PHP. Based on those examples i've created this:

1. A *.js-file containing a number of functions, of which this is one:

var handle = new Array();

function getXMLFromPOSTRequest(request_name,post_options){
	var url = "./library/ajax/ajax_post_reply.php";
	var i = handle.length;
	while (handle[i]) { i++; }
	handle[i] = getHTTPHandler();
	
	if (handle[i]) {
		handle[i].open("POST",url);
		handle[i].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		handle[i].onreadystatechange = function() {
			logJS('readystatechange!: ' + handle[i].readyState + '+' + handle[i].status);
			if (handle[i].readyState == 4 && handle[i].status == 200 ) {
				var XMLResponse = handle[i].responseXML.documentElement;
				if(XMLResponse) logJS('XMLResponse exists');
				logJS(XMLResponse.getElementsByTagName('test2')[0].firstChild.nodeValue);
				return XMLResponse;
			}
		}
		
		handle[i].send(post_options);
	} else { return false; }
}

logJS is a function which puts the given string on a new line in a log-div, so I can see what JS is doing.

2. A *.php-file which includes the previous mentioned *.js-file and calls a JS-function on clicking some text:

<script language="javascript" type="text/javascript">
function setTest() {
	var XMLResponse = getXMLFromPOSTRequest('ajax_test','burn=1&test=2');

	if (XMLResponse) getElement('ajax_stat').innerHTML = 'object exists';
	else getElement('ajax_stat').innerHTML = 'object DOESN\'T EXIST';

	var e_test = getElement('ajax_test');
	e_test.innerHTML = XMLResponse.getElementsByTagName('test2')[0].firstChild.nodeValue;

}
</script>
<span onclick="setTest();">ShowXML</span>
<div id="ajax_test"></div>
<div id="ajax_stat"></div>

3. As last, i have the *.php-file that creates the XML-ducument:

<? 
define("NL","\n");

header("Content-type: text/xml");

echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>".NL;
echo "<doc>".NL;
echo "<test2>test3</test2>".NL;
echo "<test3>test4</test3>".NL;
echo "</doc>".NL;

exit();

?>

My problem is the next: within the function getXMLFromPOSTRequest the var XMLRequest is indeed a document tree containing the result (the value of the first test2-element is being logged), but the function returns a false value (my result screams that the object DOESN'T exist). The result is that the ajax_test-div stays empty, and this text is printed inside the log-div:.
object DOESN'T exist
readystatechange!: 2+200
readystatechange!: 3+200
readystatechange!: 4+200
XMLResponse exists
test3

On several websites I found that it should be possible to pass the object as a result, but this just doesn't work. Anyone who knows that to do?

Hi,

Solved the problem myself allready. When returning XMLResponse, i was returning it from handle.onreadystatechange(), and not from getXMLFromPOSTRequest(). I fixed it by calling a numbered callback function (number i) from within the onreadystatechange()-function, where I make the changes i'd like when i click the ShowXML-text...


Greetings!
Peter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.