1

I created this PHP script that loads the contents of an XML file into a form, but I can't save it after modifying the fields.
I wanted to know if it was possible to do it, and how. Thanks Mau.

<form name="form" method="post" action="">
    <?php
    if (file_exists('data/mess.xml')) {
        $xml = simplexml_load_file('indexdata/index_messages_en.xml');
        foreach ($xml->data as $el) {
            echo "<label for='{$el}'>{$el['name']}</label>
          <input name='{$el['name']}' value='{$el}'/><br>";
        }
    } else {
        exit('Failed to open');
    }
    ?>
    <input type="submit" name="submit" id="submit" value="Update">
</form>
<?php
    if (isset($_POST['submit'])) {
        // method for save edited field in same xml file
    }
?>

My mess.xml file:

<?xmlversion = "1.0"encoding = "UTF-8"?>
<project>
    <data name="project_title">
        <![CDATA[New Project]]></data>
    <data name="section_title">
        <![CDATA[ES000021903]]></data>
</project>
Rahul
  • 18,271
  • 7
  • 41
  • 60
Maurizio
  • 21
  • 1
  • 5

1 Answers1

0

Sure you can, you'll need to rewrite the file to keep the changes from one edit to the other.

Look at file_put_contents or fopen.

Of course, you'll have to rewrite the entire XML, with tags and attributes. You can either do it in a simple string :

$myxml='
<?xmlversion = "1.0"encoding = "UTF-8"?>
<project>
    <data name="project_title">
        <![CDATA['.$_POST['project_title'].']]></data>
    <data name="section_title">
        <![CDATA['.$_POST['section_title'].']]></data>
</project>';

Or by creating a new SimpleXMLElement with your $_POST and converting it to a string, as detailled in this answer here.

Nomis
  • 854
  • 7
  • 11