0

I have a script that creates an XML file with data from a HTML form. It works great and the XML file is created and the form writes data to it. I am having a hard time appending data to the XML file though. Every time I submit new data from the form to the XML file it just overwrites the last one instead of just adding that data to it.

Here is the php script I am using to create and write the XML file from the form.

<?php

if (isset($_POST['lsr-submit']))
    {
        header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
    }

$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);

$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];

$fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
$lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
$location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
$report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
$description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);

$xml->reports = "";
$xml->reports->addChild('timestamp', date("D M jS g:i a",time()));
$xml->reports->addChild('fname', $fname);
$xml->reports->addChild('lname', $lname);
$xml->reports->addChild('location', $location);
$xml->reports->addChild('report', $report);
$xml->reports->addChild('description', $description);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test2.xml');

?>

Here is the XML output

XML FILE

HTML Form in case anyways wants to see the behavior.

HTML FORM

-Thanks!

17
  • Well.... $xml->reports = "";... What do you think that does? $report = $xml->addChild('reports'); $report->addChild('time... seems to be what you want. Commented May 31, 2013 at 23:15
  • I suggest you rename your question's name to How to add entries... instead of Append. I think people are probably misled by it. Commented Jun 1, 2013 at 0:47
  • @Fred I know how to add entries. I got this code from a link you recommended. stackoverflow.com/questions/12135467/… Which works great. BUT, I can only add one entry as the form just overwrites the entry and doesn't add to it. Commented Jun 1, 2013 at 4:07
  • @Wrikken If I don't have $xml->reports = ""; in the code it won't add anything to the XML file at all when the form is submitted. Commented Jun 1, 2013 at 4:07
  • @Texan78: Did you try my suggested replacement? Commented Jun 1, 2013 at 16:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.