Location: PHPKode > projects > HypatiaDB > hypatiadb/xhr/create-xhr.php
<?php
// This script generates a Javascript file containing various
// functions and variables for an XmlHttpRequest.
// The following needs to be passed in the query string (or
// POST data):
//
//  * target	--	the page to send the request to
//			REALLY IMPORTANT:
//			This MUST be in quotes (if it's a literal)
//
//  * caller	--	the name of the function that will be
//			called to kick off the xmlhttprequest
//
//  * tid	--	the xml:id that the data should be pasted into
//		OR
//  * tfunc	--	the function that should be called
//			when the request
//			is complete. If tfunc is set as well as tid,
//			this function will be called just before
//			the data is added to the element specified
//			in tid

header("Content-Type: text/javascript");

$vname = sprintf("x%uhr", crc32(microtime())); //a "randomish" name for the XmlHttpRequest variable

$target = $_REQUEST['target'];
$caller = $_REQUEST['caller'];
$tid = $_REQUEST['tid'];
$tfunc = $_REQUEST['tfunc'];

echo<<<ENDJS
var $vname;
function $caller() {
	if(window.XMLHttpRequest)
		$vname = new XMLHttpRequest();
	else if(window.ActiveXObject)
		$vname = new ActiveXObject("Microsoft.XMLHTTP");
	else
		alert("Browser does not (easily) support XMLHttpRequest.");
	${vname}.onreadystatechange = ${vname}orst;
	${vname}.open("GET", $target, true);
	${vname}.send("");
}

function ${vname}orst() {
	if(${vname}.readyState == 4) { //4 == OK
		if(${vname}.status != 200) {
			alert('Server returned a ' + ${vname}.status);
		} else {
ENDJS;
if(isset($_REQUEST['tid'])) {
	if (isset($_REQUEST['tfunc'])) {
		echo("${tfunc}"); //(${vname}.responseXML);");
	}
	//we need to move the root node of the responseXML into the element with
	//the id specified by tid
	echo<<<ENDJS
var parent = document.getElementById('$tid');
var src = ${vname}.responseXML.firstChild;
var nn = document.importNode(src, true);
parent.appendChild(nn);
ENDJS;
} else if (isset($_REQUEST['tfunc'])) {
	echo("${tfunc}(${vname}.responseXML);");
}

echo('}}}');
Return current item: HypatiaDB