Texan78
Texan78

Reputation: 687

Creating XML file from HTML form

Ok so I got it working, now the only problem is when a new submission is added it overwrites the previous entry. I need it to add the newest submission to the XML file and not over ride it and store it for X amount of time.

Here is the working php script that creates the xml file and takes the data from the HTML form and puts it in the XML file.

<?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('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 file it creates.

XML FILE

Here is the form to submit to the XML file. Submit a test submission and it takes you to the page to display but you'll notice it will overwrite the last one instead of adding to the XML file.

HTML FORM

Upvotes: 3

Views: 25523

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74232

This is what I came up with and works well, and tested.

NOTE: However if the file (file.xml) does not exist, it will throw off an error, so if you figure out a way to automatically delete the old file(s) via CRON or any other method (you mentioned: "...and store it for X amount of time."), you'll have to come up with a way to make a pre-built structured file with at least one set of entries inside it.

E.g.:

<?xml version="1.0" encoding="UTF-8"?>
<entries>
  <reports>
    <timestamp>May 31, 2013, 11:56 am</timestamp>
    <fname>Fred</fname>
    <lname>Fletcher</lname>
    <location>Canada</location>
    <report>Wind Damage</report>
    <description>Winds were gusting mighty hard today!</description>
  </reports>
</entries>

This is relatively easy to do, I've done it before with an if file exists....

Here is my working code:

<?php

// Script by Fred Fletcher, Canada.

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

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('file.xml');

$element = $xml->getElementsByTagName('reports')->item(0);

$timestamp = $element->getElementsByTagName('timestamp')->item(0);
$fname = $element->getElementsByTagName('fname')->item(0);
$lname = $element->getElementsByTagName('lname')->item(0);
$location = $element->getElementsByTagName('location')->item(0);
$report = $element->getElementsByTagName('report')->item(0);
$description = $element->getElementsByTagName('description')->item(0);

$newItem = $xml->createElement('reports');

$newItem->appendChild($xml->createElement('timestamp', date("F j, Y, g:i a",time())));;

$newItem->appendChild($xml->createElement('fname', $_POST['firstname']));
$newItem->appendChild($xml->createElement('lname', $_POST['lastname']));
$newItem->appendChild($xml->createElement('location', $_POST['location']));
$newItem->appendChild($xml->createElement('report', $_POST['report']));
$newItem->appendChild($xml->createElement('description', $_POST['desc']));

$xml->getElementsByTagName('entries')->item(0)->appendChild($newItem);

$xml->save('file.xml');

echo "Data has been written.";

?>

A "plug" as a comment in the script would be nice, "Script by Fred Fletcher, Canada." (wink)

Let me know how this works out for you.

Upvotes: 1

user2426701
user2426701

Reputation:

I am not sure I got your question, but if you need to create a file that is unique for each submission then you cannot simply use echo, you must actually create a file.

Also if after submitting you redirect to another PHP file, how can you handle the data to write into the variables?

Example for creating a file:

<?php // testfile.php
$fh = fopen("testfile.xml", 'w') or die("Failed to create file"); 
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($fh, $text) or die("Could not write to file"); 
fclose($fh);
?>

Upvotes: 0

Related Questions