: Hi all,
I have a form where you can add an activity and using php dom insert it into a xml file.
The code below is not working, it does not insert the information in the xml file and I don't konw why.
Thanks for your help!
I have a form where you can add an activity and using php dom insert it into a xml file.
The code below is not working, it does not insert the information in the xml file and I don't konw why.
Thanks for your help!
Code:
/// the xml file /// <list> <activity>swimming</activity> <activity>running</activity> <list> ///// index.php ///// <?php echo" <html> <head><title>test</title></head> </head> "; $xmldoc = new DOMDocument(); $xmldoc->load('sample.xml', LIBXML_NOBLANKS); $activities = $xmldoc->firstChild->firstChild; if($activities!=null){ while($activities!=null){ echo $activities->textContent.'<br/>'; $activities = $activities->nextSibling; } } echo" <body> <form name='input' action='insert.php' method='post'> Insert activity: <input type='text' name='activity'/> <input type='submit' value='send'/> </form> </body> </html "; ?> ////// insert.php ////////// <?php header('Location:index.php'); $xmldoc = new DOMDocument(); $xmldoc->load('sample.xml'); $newAct = $_POST['activity']; $root = $xmldoc->firstChild; $newElement = $xmldoc->createElement('activity'); $root->appendChild($newElement); $newText = $xmldoc->createTextNode($newAct); $newElement->appendChild($newText); $xmldoc->save('sample.xml'); ?>
Comment