1

I'm trying to make a script to read an XML file, insert it in a form and edit and update the values using php.

The form loads the present data, but does not update them.

Can anyone tell me a tip? Thanks Maurizio

file XML

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

  <data name="en_title"><![CDATA[mytitle]]></data>
  <data name="en_scene_title"><![CDATA[ES000021903]]></data>

</myxml>

file edit PHP

<?php
$form_fields  = null;
$data = simplexml_load_file('messages_en.xml');
foreach($data->data as $field)
{

  $form_fields .= '<div>';
    $form_fields .=  '<label>' .$field['name'] . ' </label>';
    $form_fields .=  '<input type="text" id="' .$field['name'] . '"placeholder="'.htmlentities($field).'" name="'.$field. '" />';
    $form_fields .= '</div>';
  }
  ?>
  <!DOCTYPE html>
  <html>
  <head>
    <style>
      form { font-size: 16px; }
      form div { margin: .3em; }
      legend { font-weight: bold; font-size: 20px; }
      label { display:inline-block; width: 140px; }
    </style>
  </head>
  <body>
    <form method="POST" action="process.php">
      <legend> Enter Contact Information</legend>
      <?php echo $form_fields; ?>
      <input type="submit" name="submit" value="Upload" class="btn"  />
    </form>
  </body>
  </html>

code file process.php

<?php
    $xml = file_get_contents('messages_en.xml');
    $sxml = simplexml_load_string($xml);
    if(isset($sxml->item[$_POST['name']])) {
        $node->data = $_POST['name'];
    }
    file_put_contents('messages_en.xml', $sxml->asXML());
?>
4
  • Better use DOMDocument Commented Nov 23, 2019 at 18:15
  • 1
    @LarsStegelitz nothing wrong with using SimpleXML. Commented Nov 23, 2019 at 18:22
  • What is $node? Commented Nov 23, 2019 at 20:40
  • Also, I'm not familiar with SimpleXML but isn't $sxml going to contain an array of the two data elements? Commented Nov 23, 2019 at 20:42

1 Answer 1

1

Do you want to update the XML based on the name attribute of the <data> element?

If so, you need to a) pass both the name and the value from your HTML, and b) search for the name you want.

$var = $_POST["name"];
$val = $_POST["value"];

$xml = file_get_contents("messages_en.xml");
$dom = new DomDocument();
$dom->loadXml($xml);
$xpath = new DomXpath($dom);
// find the data element with the matching attribute
$node = $xpath->query("/myxml/data[@name='$var']");
// assume there's only one, otherwise we can loop
// clear the existing content
$node[0]->textContent = "";
// create a new CDATA section
$node[0]->appendChild($dom->createCDATASection($val));
// save the updated XML
file_put_contents("messages_en.xml", $dom->saveXml());
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply, I need to change the value inside tag ( in bold ) example : <data name="en_title"><![CDATA[mytitle]]></data> is it possible to do so?, unfortunately the xml file is not generated by me and I cannot change it. thx
You don't really need the CDATA section, but there it is anyway.
The script works, but not in my server, the module does not pass the variables from the module ... what could it be?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.