View Full Version : Returning XML data

11-09-2007, 06:14 PM
I'm trying to use PHP to return an XML document, but I am getting an error saying that the response XML has no properties. When I manually send it to an xml page with hardcoded data though, it works how it's supposed to. I assume this means I am missing a header somewhere to tell the ajax call to treat the php page like an xml document.

This is what the ajax call looks like:

xmlhttp.open("GET", "ajx_update_contacts.php", true);

And with the return data..

var XMLDoc = xmlhttp.responseXML;
var Nodes = XMLDoc.getElementsByTagName("CONTACT");
var NumNodes = Nodes.length;

contactsArray.length = 0;

for (var i = 0; i < NumNodes; i++)
var node_Name = Nodes[i].getElementsByTagName("NAME")[0].firstChild.nodeValue;
var node_ID = Nodes[i].getElementsByTagName("ID")[0].firstChild.nodeValue;
var node_Status = Nodes[i].getElementsByTagName("STATUS")[0].firstChild.nodeValue;

var newContact = new contact(node_Name, node_ID, node_Status);
contactsArray[contactsArray.length] = newContact;

And the php code to create the XML document:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-Type: text/xml; charset=utf-8");

$XML = '<?xml version="1.0"?>';
$XML .= "<ROOT>";

while($contactID = mysql_fetch_array($queryContacts))
$contact_id = $contactID[0];

$queryContact = mysql_query("SELECT name, status FROM members WHERE member_id = '" . $contact_id . "'") or die(mysql_error());
$contactData = mysql_fetch_array($queryContact)

$contact_name = $contactData[0];
$contact_status = $contactData[1];

$XML .= "<CONTACT><NAME>" . $contact_name . "</NAME><ID>" . $contact_id . "</ID><STATUS>" . $contact_status . "</STATUS></CONTACT>";

$XML .= "</ROOT>";

echo $XML;

11-09-2007, 06:38 PM
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );

though that looks a bit fishy... did u try accessing the ajx_update_contacts.php page in a browser? does it display like any other static xml file?

the php script may be silently dying out

11-09-2007, 10:00 PM
With that header taken out and with no headers at all, there is no display when I open ajx_update_contacts.php. What could be causing this?