maddenuk
06-04-2007, 10:44 PM
I've been working on this for ages but i am trying to output some XML using Javascript. The code i got below i thought would output the content for the tag element <content />. However my code produces a JavaScript alert error which is an error reading the response - Type Error Null value Returned.
The XML Returned is valid.
Can anyone help or give advice which is the simplest and effective way of output the data from the PHP Generated XML file?
Thanks
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
{
try
{
xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
}
catch(e) {}
}
}
if(!xmlHttp)
alert("Error Creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process()
{
if(xmlHttp)
{
try
{
//get values from option tag
var id = document.getElementById("country").value;
//create params string
var params = "country="+id;
//initate the asynchronous HTTP request
xmlHttp.open("GET", "country-find.php?" + params, true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch(e)
{
alert("Can't connect to the server:\n" + e.toString());
}
}
}
function handleRequestStateChange()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
try
{
handleServerResponse();
}
catch(e)
{
alert("Error reading the response: " + e.toString());
}
}
else
{
alert("There was a problem retreiving the data:\n" + xmlHttp.statusText);
}
}
}
function handleServerResponse()
{
var xmlResponse = xmlHttp.responseXML;
//catching potential errors with IE and Opera
if(!xmlResponse || !xmlResponse.documentElement)
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
//catching potential errors with Firefox
var rootNodeName = xmlResponse.documentElement.nodeName;
if(rootNodeName == "parsererror")
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
//obtain the XML documents element
xmlRoot = xmlResponse.documentElement;
//testing that we received the XML document we expect
if(rootNodeName != "response" || !xmlRoot.firstChild)
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
responseText = xmlRoot.getElementsByTagName('content');
outputText = document.write(responseText);
//display the user message
myDiv = document.getElementById("myDivElement");
myDiv.innerHTML = outputText;
}
<?php
ini_set('output_buffering', false);
ini_set('implicit_flush', true);
require_once('error_handler.php');
require_once('classes/database.class.php');
header('Content-Type: text/xml');
$country = $_GET['country'];
$table_id = 'country';
$connection = new database();
$link = $connection->database_connection();
$query = "select * from country where location ='$country'";
$results = mysql_query($query) or die(mysql_error());
if (!$results)
{
print 'There was a database error when executing';
print mysql_error();
exit;
}
//create new xml document
$doc = new DomDocument();
// create root node
$root = $doc->createElement('response');
$root = $doc->appendChild($root);
//process one row at a time
while($row = mysql_fetch_assoc($results)) {
//add node for each row
$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
//add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} //foreach
} //while
$xml_string = $doc->saveXML();
echo trim($xml_string);
The XML Returned is valid.
Can anyone help or give advice which is the simplest and effective way of output the data from the PHP Generated XML file?
Thanks
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
{
try
{
xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
}
catch(e) {}
}
}
if(!xmlHttp)
alert("Error Creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process()
{
if(xmlHttp)
{
try
{
//get values from option tag
var id = document.getElementById("country").value;
//create params string
var params = "country="+id;
//initate the asynchronous HTTP request
xmlHttp.open("GET", "country-find.php?" + params, true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}
catch(e)
{
alert("Can't connect to the server:\n" + e.toString());
}
}
}
function handleRequestStateChange()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
try
{
handleServerResponse();
}
catch(e)
{
alert("Error reading the response: " + e.toString());
}
}
else
{
alert("There was a problem retreiving the data:\n" + xmlHttp.statusText);
}
}
}
function handleServerResponse()
{
var xmlResponse = xmlHttp.responseXML;
//catching potential errors with IE and Opera
if(!xmlResponse || !xmlResponse.documentElement)
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
//catching potential errors with Firefox
var rootNodeName = xmlResponse.documentElement.nodeName;
if(rootNodeName == "parsererror")
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
//obtain the XML documents element
xmlRoot = xmlResponse.documentElement;
//testing that we received the XML document we expect
if(rootNodeName != "response" || !xmlRoot.firstChild)
throw("Invalid XML Structure:\n" + xmlHttp.responseText);
responseText = xmlRoot.getElementsByTagName('content');
outputText = document.write(responseText);
//display the user message
myDiv = document.getElementById("myDivElement");
myDiv.innerHTML = outputText;
}
<?php
ini_set('output_buffering', false);
ini_set('implicit_flush', true);
require_once('error_handler.php');
require_once('classes/database.class.php');
header('Content-Type: text/xml');
$country = $_GET['country'];
$table_id = 'country';
$connection = new database();
$link = $connection->database_connection();
$query = "select * from country where location ='$country'";
$results = mysql_query($query) or die(mysql_error());
if (!$results)
{
print 'There was a database error when executing';
print mysql_error();
exit;
}
//create new xml document
$doc = new DomDocument();
// create root node
$root = $doc->createElement('response');
$root = $doc->appendChild($root);
//process one row at a time
while($row = mysql_fetch_assoc($results)) {
//add node for each row
$occ = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
//add a child node for each field
foreach ($row as $fieldname => $fieldvalue) {
$child = $doc->createElement($fieldname);
$child = $occ->appendChild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} //foreach
} //while
$xml_string = $doc->saveXML();
echo trim($xml_string);