Getting javascript posted XML in PHP??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wrathrowe
Forum Newbie
Posts: 7
Joined: Fri Feb 19, 2010 6:18 am

Getting javascript posted XML in PHP??

Post by Wrathrowe »

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:

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;

?>

User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Getting javascript posted XML in PHP??

Post by requinix »

Try sending the XML string, not the DOMDocument it created.

Code: Select all

xmlHttp.send(xml);
Wrathrowe
Forum Newbie
Posts: 7
Joined: Fri Feb 19, 2010 6:18 am

Re: Getting javascript posted XML in PHP??

Post by Wrathrowe »

It worked!! Thank you so much!
Post Reply