html - Save form data to XML using PHP with overwrite feature -
i'm trying make form data save in exertnal xml file. xml file exists , populated data.
i need save form data xml file needs add new record everytime except if section in xml file same new form submission. if happens, needs modify record in xml file had same data.
so if someones names mario bros , fills form first time, create new record in xml file. if same mario bros re-fill form same name, different "location" in form, overwrite existing mario bros in xml file.
here's html code :
<form> <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>
here's php code
<?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'); ?>
what doing wrong? can't seem find way make work. lot!
Comments
Post a Comment