Jump to content

Updating a new XML element


thebl
 Share

Recommended Posts

I'm working on an Ajax project for a simple auction site. I have an html page with two textfields, one were a user can enter the itemNumber of a product they want to bid for and the other where they can enter a new bid. I'm using JavaScript to validate and to send the inputs to the PHP file.But I cannot figure how to update the newBid element value currently in the XML file with the new value which the user has inputed!And this is PHP file I've playing with and still cannot figure how to update the newBid element with a new text value. I've tried using replaceChild, but with little success. If anyone can help it would be much appreciated!!!

<?phpsession_start();header('Content-Type: text/xml');?><?php		$itemNumber = $_GET["itemNumber"];	$newBid = $_GET["newBid"];				// $doc = DOMDocument::load('./XML/auction.xml');		$doc = new DomDocument('1.0');		$doc->load("../XML/auction.xml");		$root = $doc->documentElement;		$AuctionItem = $doc->getElementsByTagName("AuctionItem");	  $iNumber = $node->getElementsByTagName("itemNumber");		$iNumber = $iNumber->item(0)->nodeValue;	$inewBid = $node->getElementsByTagName("newBid");		$inewBid = $inewBid->item(0)->nodeValue;			  		if ($iNumber == $itemNumber){ // So when the user enters the itemNumber, it should match the itemNumber in the XML file			 //Dont know know how to update the text content of the newBid element with new text					$doc->save("../XML/auction.xml"); 		$strXml = $doc->saveXML(); 		ECHO ($strXml);		}?>

Link to comment
Share on other sites

Things are a lot easier (and very often faster) when you use XPath:

<?php	$itemNumber = $_GET["itemNumber"];	$newBid = $_GET["newBid"];	$xmlFile = '../XML/auction.xml';	// $doc = DOMDocument::load('./XML/auction.xml');	$doc = new DOMDocument;	$doc->load($xmlFile);	$xpath = new DOMXPath($doc);	//The next line alone selects the newBid element that is in an AuctionItem with a certain itemNumber	$newBidNode = $xpath->query('/AuctionList/AuctionItem[itemNumber = ' . $itemNumber  . ']/newBid');	//nodeValue is not a read only property, meaning you can adjust it as done below	$newBidNode->item(0)->nodeValue = $newBid;	//The results are still written within the $doc, so once we're done, saving is still needed and done on the $doc	$doc->save($xmlFile);	//Every time you invoke a save() or saveXML() function, you invoke the XML serializer.	//Doing so twice is somewhat performance costly and needless unless you wanted to adjust the serialization options.	//Here's an alternative.	echo file_get_contents($xmlFile);?>

Be sure to filter out the $itemNumer input first though, as it could lead to "XPath injections" in the same way SQL queries lead to SQL injections.

Link to comment
Share on other sites

Things are a lot easier (and very often faster) when you use XPath:
<?php	$itemNumber = $_GET["itemNumber"];	$newBid = $_GET["newBid"];	$xmlFile = '../XML/auction.xml';	// $doc = DOMDocument::load('./XML/auction.xml');	$doc = new DOMDocument;	$doc->load($xmlFile);	$xpath = new DOMXPath($doc);	//The next line alone selects the newBid element that is in an AuctionItem with a certain itemNumber	$newBidNode = $xpath->query('/AuctionList/AuctionItem[itemNumber = ' . $itemNumber  . ']/newBid');	//nodeValue is not a read only property, meaning you can adjust it as done below	$newBidNode->item(0)->nodeValue = $newBid;	//The results are still written within the $doc, so once we're done, saving is still needed and done on the $doc	$doc->save($xmlFile);	//Every time you invoke a save() or saveXML() function, you invoke the XML serializer.	//Doing so twice is somewhat performance costly and needless unless you wanted to adjust the serialization options.	//Here's an alternative.	echo file_get_contents($xmlFile);?>

Be sure to filter out the $itemNumer input first though, as it could lead to "XPath injections" in the same way SQL queries lead to SQL injections.

Thanks for your help, and another thanks for the detailed explaination!!!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...