thebl Posted September 16, 2007 Share Posted September 16, 2007 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 More sharing options...
boen_robot Posted September 16, 2007 Share Posted September 16, 2007 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 More sharing options...
thebl Posted September 17, 2007 Author Share Posted September 17, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.