Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting data in XML file with PHP DOM
    primarykey
    data
    text
    <p>I was trying to insert new data into an existing XML file, but it's not working. Here's my xml file:</p> <pre><code>&lt;list&gt; &lt;activity&gt;swimming&lt;/activity&gt; &lt;activity&gt;running&lt;/activity&gt; &lt;list&gt; </code></pre> <p>Now, my idea was making two files: an index page, where it displays what's on the file and provides a field for inserting new elements, and a php page which will insert the data into the XML file. Here's the code for index.php:</p> <pre><code>&lt;html&gt; &lt;head&gt;&lt;title&gt;test&lt;/title&gt;&lt;/head&gt; &lt;/head&gt; &lt;?php $xmldoc = new DOMDocument(); $xmldoc-&gt;load('sample.xml', LIBXML_NOBLANKS); $activities = = $xmldoc-&gt;firstChild-&gt;firstChild; if($activities!=null){ while(activities!=null){ echo $activities-&gt;textContent.'&lt;br/&gt;'; activities = activities-&gt;nextSibling. } } ?&gt; &lt;form name='input' action='insert.php' method='post'&gt; insert activity: &lt;input type='text' name='activity'/&gt; &lt;input type='submit' value='send'/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html </code></pre> <p>and here's the code for insert.php:</p> <pre><code>&lt;?php header('Location:index.php'); $xmldoc = new DOMDocument(); $xmldoc-&gt;load('sample.xml'); $newAct = $_POST['activity']; $root = $xmldoc-&gt;firstChild; $newElement = $xmldoc-&gt;createElement('activity'); $root-&gt;appendChild($newElement); $newText = $xmldoc-&gt;createTextNode($newAct); $newElement-&gt;appendChild($newText); $xmldoc-&gt;save('sample.xml'); ?&gt; </code></pre> <p>The user is to access index.php, where he would see a list of the current activities present in the XML file, and a text field below where he can insert new activities. Upon clicking the send button, the page would call insert.php, which contains a code that opens the XML file in a DOM tree, inserts a new node under the root node and calls back the index.php page, where the user should be able to see the list of activities, his new activity there under the others. It is not working. When i click on the button to submit a new entry, the pages refreshes and apparently nothing happens, the XML is the same as before. What did i do wrong? Also, i'd like to know if there's a better way of doing it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload