Can I pass an xml file to JavaScript by PHP like this:
<?php
$xmlDoc = new DOMDocument('1.0');
$xmlDoc->formatOutput = true;
$xmlDoc->load("customer.xml");
echo $xmlDoc->saveXML();
$strXml = $xmlDoc->saveXML();
return $strXml;
?>
when I try to do this I get null in the server response in this line in JavaScript
var serverResponse = xHRObject.responseXML;
but,
var spantag = document.getElementById("example").innerHTML =
xHRObject.responseText;
will print out the information and also prompt(spantag); will prompt the whole xml document. But I want to use the tag names. So that I need to use
var header = serverResponse.getElementsByTagName("book");
but this gives me an error since the xHRObject.responseXML is null. Please tell me what I'm doing wrong?
var xHRObject = false;
if (window.XMLHttpRequest)
{xHRObject = new XMLHttpRequest();}
else if (window.ActiveXObject)
{xHRObject = new ActiveXObject("Microsoft.XMLHTTP");}
function validate(){
xHRObject.open("GET", "login.php", true);
xHRObject.onreadystatechange =test;
xHRObject.send(null); }
function test(){
if ((xHRObject.readyState == 4) &&(xHRObject.status == 200))
{var serverResponse = xHRObject.responseXML;
var header = serverResponse.getElementsByTagName("customer");
var spantag = document.getElementById("err")= xHRObject.responseText;}}
I can output via the prompt the span tag it'll give me a xml file but when I output serverResponse it'll give a null. that means responseText have a value but response xml doesn't.
Customer.xml has customers as the main tag and then I have added two customers in a customer tag. then firstname surname email tags are there.