0

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.

2 Answers 2

1

Try using echo $xmlDoc instead of a simple return, which doesn't actually generate any content. Passing it VIA an ajax call (which I'm assuming you are) requires that you echo or print some content on the called page.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm trying to print it using javascript not with the php. I just want to get the values to javascript through the php. However I tried what you told it still gave the same error.
I'd need more code to go on to give a more thorough answer.
If you could expand on the code: I.E. What file is the PHP in? Where is the JavaScript located? Are you attempting to supply the XML inside the same file as you're attempting to manipulate it with the javascript? Or are you attempting to use an AJAX call to retrieve the XML from a different file?
I have everything in one folder. customer.xml is in the same folder. I extended my question please help me in this.
rest of the file names are login.htm login.js login.php
|
0

Unfortunately I can't use jQuery for this.

I found the answer to the question. Actually some one taught me the problem was in my php file. I had to add the header text to that:

header('Content-Type: text/xml');

1 Comment

Would you please show your completed script to pass XML from PHP to JS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.