Getting javascript posted XML in PHP??
Posted: Thu Nov 04, 2010 12:58 pm
Hey everyone, I'm trying to create an XML document in Javascript and Post it to PHP when I can read the XML document and parse through it. I'm assuming I have the document created correctly in Javascript, but I don't know how to read it into PHP. Any help would be extremely helpful, it seems like this shouldn't be as hard as I'm finding it. Here is a sample of my code. Thank you.
Javascript:
var xml = '<xml><testNode>Testing</testNode></xml>';
parser = new DOMParser();
xmlDoc = parser.parseFromString(xml,"text/xml");
if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
xmlHttp.open('POST', 'submit.php', true);
xmlHttp.setRequestHeader("Content-Type", "text/xml");
xmlHttp.onreadystatechange = getSubmitResponse;
xmlHttp.send(xmlDoc);
}
PHP:
Javascript:
var xml = '<xml><testNode>Testing</testNode></xml>';
parser = new DOMParser();
xmlDoc = parser.parseFromString(xml,"text/xml");
if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
xmlHttp.open('POST', 'submit.php', true);
xmlHttp.setRequestHeader("Content-Type", "text/xml");
xmlHttp.onreadystatechange = getSubmitResponse;
xmlHttp.send(xmlDoc);
}
PHP:
Code: Select all
<?php
header('Content-Type: text/xml'); //set the header type to XML
$raw_xml = file_get_contents("php://input");
$xmlData = new SimpleXMLElement($raw_xml);
$dom = new DOMDocument(); //create a new DOMDocument which the XML info will be stored
$dom->load($xmlData);
$xmlString = $dom->saveXML();
echo $xmlString;
?>