I'm new to new to PHP. I'm trying to create an XML document from HTML form values.
Here is the PHP code:
<?php
if (isset($_POST['lsr-submit']))
{
header('Location: http://movie1b.tk');
}
$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', 'a');
?>
And here's the HTML code:
<form name="lsrReports" action="test.php" method="post">
<table width="50%" align="center" cellpadding="2" cellspacing="0">
<tr>
<td> First name:</td><td> <input type="text" name="firstname"></td>
<td> Last name:</td><td> <input type="text" name="lastname"></td>
</tr>
<tr>
<td> Location:</td><td> <input type="text" name="location"></td>
<td> Report:</td><td> <select name="report">
<option value="Wind Damage" selected>Wind Damage</option>
<option value="Hail">Hail</option>
<option value="Flooding">Flooding</option>
<option value="Power Outage">Power Outage</option>
<option value="General">General</option>
</select>
</td>
</tr>
<tr>
<td> Description: </td><td colspan="4"> <textarea rows="5" cols="65" name="desc" onfocus="this.value=''">Enter report description</textarea></td>
</tr>
<tr>
<td colspan="4" style="text-align:center;"><input type="submit" name="lsr-submit" value="Submit"></td>
</tr>
</table>
</form>
Every time I press the submit button it overwrites the existing XML. I was wondering how do I save the users values without overwriting the XML and if the file already existing i want it to add the user values at the top of the existing xml
Thank you guys.
Instead of creating a new XML file each time, you'll want to load your existing XML file. To reuse as much of your code as possible, use SimpleXML's file-reading function:
// This file "test.xml" is pre-populated with your base XML:
// <?xml version="1.0" encoding="UTF-8"?><entrys></entrys>
$xml = simplexml_load_file('test.xml');
We need to make a change to your schema; <entrys>
should be an array-like node of <entry>
nodes. Correct? If so, replace your current addChild
methods by creating a parent node to capture the first name, last name etc.:
$entry = $xml->addChild('entry');
$entry->addChild('fname', $fname);
$entry->addChild('lname', $lname);
$entry->addChild('location', $location);
$entry->addChild('report', $report);
$entry->addChild('description', $description);
Running this twice, we'll get something like:
<?xml version="1.0" encoding="UTF-8"?>
<entrys>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
</entrys>