-3

I have an XML file held on a server as below. I then have an HTML file with a button that when pressed must plus one (+1) to one of my XML node values.

Im not too clued up on php so any help would be great. but i need a simple script that is stored on the server that will take in an html request and add 1 to my chosen XML node value.

<?xml version="1.0" encoding="UTF-8"?>

<object1>
    <value>10</value>
</object1>

<object2>
    <value>6</value>
</object2>
madth3
  • 7,275
  • 12
  • 50
  • 74
Dustin Silk
  • 4,320
  • 5
  • 32
  • 48

1 Answers1

0

Try something like this:

$objectX = "2"; // You get this value with $_POST or $_GET ...
$xmlFileName = 'my.xml'; // You're XML file

$xmlFile = file_get_contents($xmlFileName); // Saving the XML contents in a variable
$objects = new SimpleXMLElement($xmlFile);

$objectX = "object".$objectX; // The object name
$objects->$objectX->value++; // Incrementing the value
$objects->asXML($xmlFileName); // Saving the XML

echo $objects->$objectX->value; // echo the value

You have to add <objects></objects> to your XML file:

<?xml version="1.0" encoding="UTF-8"?>
<objects>
    <object1>
        <value>10</value>
    </object1>
    <object2>
        <value>6</value>
    </object2>
</objects>
HamZa
  • 14,671
  • 11
  • 54
  • 75