1

I am trying to to do an AJAX call from javascript to a PHP File, that will update a database, then return to the original java script function XML data. I think the disconnect i am having is getting the php to output correctly to be read as XML in the function. Please see the code i have below. ---EDIT--- Simpler code to find heart of issue.

--ANSWERED-- The PHP File has to have the header set as XML.

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

Java Script:

function AddNewUser(){
document.getElementById("overlay").innerHTML="<span>Started...</span>";
aj_test=new XMLHttpRequest();
aj_test.open("POST","test.php",true);
aj_test.setRequestHeader("Content-type","application/x-www-form-urlencoded");
alert("pre");
aj_test.onreadystatechange=function()
{
    if (aj_test.readyState==4 && aj_test.status==200)
    {
        XMLReturn=aj_test.responseXML;
        alert(XMLReturn.getElementsByTagName("title")[0].childNodes[0].nodeValue);
    }

}
aj_test.send("");

}

PHP:

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo $doc->saveXML();
4
  • You need to escape your URL parameters. Commented Aug 16, 2013 at 14:21
  • It looks like you have all the ingredients you need, so I suspect you're right and that the issue concerns how you're passing the XML back to the javascript client. How are you echoing or emitting $XMLData, and what data does the javascript client actually receive? Commented Aug 16, 2013 at 14:56
  • 1
    Updated code, with simple static string for testing. if u use responseText; then i get all the information as formated correctly as XML, but i need it passed as XML so i can find specific data. Commented Aug 16, 2013 at 15:00
  • Glad you got to the heart of the matter. I believe I've found my way through the same problem once or twice before! Commented Aug 16, 2013 at 15:19

1 Answer 1

1

--ANSWERED-- The PHP File has to have the header set as XML.

header ("Content-Type:text/xml");
Sign up to request clarification or add additional context in comments.

Comments

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.