jquery - replace parts of xml using php -
i'm trying post data via ajax php file writes xml. want post bits of data , replace part of xml.
the js:
$(".asave").click(function(){ var name = $(this).attr("data-name"); var value = $(this).attr("data-value"); var dataobj = {}; dataobj[name]=value; $.ajax({ url: 'view/template/common/panel.php', type: 'post', data: dataobj, }).done(function(){ alert("saved!"); }); });
the php:
<?php $fruits [] = array( $orangesvalue = $_post['oranges'], $applesvalue = $_post['apples'], ); $doc = new domdocument(); $doc->formatoutput = true; $maintag = $doc->createelement( "fruits" ); $doc->appendchild( $maintag ); $oranges = $doc->createelement("oranges"); $oranges->appendchild($doc->createtextnode($orangesvalue)); $maintag->appendchild($oranges); $apples = $doc->createelement("apples"); $apples->appendchild($doc->createtextnode($applesvalue)); $maintag->appendchild($apples); echo $doc->savexml(); $doc->save("fruits.xml") ?>
so when send oranges value writes new xml file containing oranges value only. possible replace value i'm posting.
cheers
so if understand correctly, want read existing xml-document, replace text content of oranges
element , save document again?
try then:
$filename = '/my/absolute/path/to/my/file.xml'; $doc = new domdocument(); $doc->load($filename); $nodes = $doc->getelementsbytagname('oranges'); if (null != $nodes) { $nodes->item(0)->nodevalue = $_post['oranges']; } $doc->save($filename);
but overly complicated (or me hating dom extension). if have simplexml extension available, try this:
$filename = 'blabla.xml'; $xml = simplexml_load_file($filename); $xml->oranges = $_post['oranges']; $xml->asxml($filename);
of course you'd need check $_post
data correctness before writing in xml file. if want act based on whether posted apples or oranges, replace oranges
variable:
foreach ($_post $fruit => $val) { $xml->{$fruit} = $val; }
Comments
Post a Comment