Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 53289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:02:24+00:00 2026-05-10T17:02:24+00:00

I was trying to insert new data into an existing XML file, but it’s

  • 0

I was trying to insert new data into an existing XML file, but it’s not working. Here’s my xml file:

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

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:

<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’s 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 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:02:25+00:00Added an answer on May 10, 2026 at 5:02 pm

    is your code block copy and pasted from your existing files? if so i see two potential issues:

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

    note: you’re missing action='insert.php', which would cause the form to just reload itself without submitting, which is the behaviour you describe.

    secondly, make sure you have write permission to ‘sample.xml’. you can confirm if you’re actually writing anything:

    print 'I wrote '.$xmldoc->save('sample.xml').' bytes of data'; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert some import lines into a python source file, but i
I'm trying to insert into XML column (SQL SERVER 2008 R2), but the server's
I'm trying to insert some new objects into a firebird database using NHibernate. I
Trying to do an insert, I have: jdbcTemplate.update(insert into...., new Object[]{foo.getId(), foo.getName()}) foo.getId() returns
I'm trying to insert a column into an existing DataSet using C#. As an
I am trying to get the DataGridView to render the insert new row row
I am trying to INSERT INTO a table using the input from another table.
I am trying to insert about 50,000 objects (and therefore 50,000 keys) into a
I am trying to insert a time only value, but get the following error
I'm trying to insert a comment character into a string something similar to this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.