Insert data into an XML file using PHP DOM

I tried to insert new data into an existing XML file, but it does not work. Here is my xml file:

<list> <activity>swimming</activity> <activity>running</activity> <list> 

Now my idea was to create two files: an index page, where it displays what is in the file, and provides a field for inserting new elements, and also a php page that will insert data into the XML file. Here's the index.php code:

 <html> <head><title>test</title></head> </head> <?php $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. } } ?> <form name='input' action='insert.php' method='post'> insert activity: <input type='text' name='activity'/> <input type='submit' value='send'/> </form> </body> </html 

and here is the code for 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'); ?> 

The user should access index.php, where he will see a list of the current actions present in the XML file, and the text box below where he can insert new actions. After clicking the submit button, the page will call insert.php, which contains the code that opens the XML file in the DOM tree, inserts a new node under the root of the node and calls the index.php page, where the user should be able to see a list of activities, his new activity there under by others. This does not work. When I press the button to send a new record, the pages are updated and apparently nothing is happening, the XML is the same as before. What have I done wrong? Also, I would like to know if there is a better way to do this.

+6
dom xml php
source share
6 answers

is your copy of code and pasted from your existing files? if so, I see two potential problems:

 <form name='input' action'insert.php' method='post'> // should be: <form name="input" action="insert.php" method="post"> 

Note: you are missing action = "insert.php" , which will cause the form to simply reload without submitting, this is the description you describe.

secondly, make sure you have write permission to "sample.xml". you can confirm that you are actually writing something:

 print 'I wrote '.$xmldoc->save('sample.xml').' bytes of data'; 
+8
source share

Final decision

sample.xml

 <list> <activity>swimming</activity> <activity>running</activity> <activity>Jogging</activity> <activity>Theatre</activity> <activity>Programming</activity> </list> 

index.php

 <html> <head><title>test</title></head> </head> <?php $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; } } ?> <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'); ?> 
+3
source share

$ newText = $ xmldoc-> createTextNode ($ newActv);

Change this line to

$ newText = $ xmldoc-> createTextNode ($ newAct);

+2
source share

In fact, you made mistakes in two places.

This line should be I think because of a typo you missed the equal sign. Also

These lines should be

Try it now, it should work, hop it will make sense

+2
source share

I think I know what the problem is with your code. You should not write like this: <?xml-stylesheet type="text/xsl" href="sample.xsl" ?> correct code is:

 <?xml:stylesheet type="text/xsl" href="sample.xsl" ?> 
+1
source share

this is the code i work for me.

index.php

 <html> <head><title>test</title></head> </head> <?php $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; } } ?> <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'); ?> 

sample.xml

 <list> <activity>swimming</activity> <activity>running</activity> </list> 
+1
source share

All Articles