Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Append XML file with php

$
0
0

I have a php 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

 

http://www.mesquiteweather.net/xml/test2.xml

 

HTML Form in case anyways wants to see the behavior after new data is submitted.

 

http://www.mesquiteweather.net/wxmesqLSR-report.php

 

-Thanks!


Viewing all articles
Browse latest Browse all 13200

Trending Articles