By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

trouble with php/xml edit via form

P: 25
Hello
I am trying to use php to add nodes to an xml file based on user input
The problem is that the xml ends up having data inserted into it in addition to the data I asked the php to insert

Here are the codes:

for SAMPLE.XML
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <list>
  3.     <activity><name>swimming</name><votes>2</votes></activity>
  4.     <activity><name>running</name><votes>3</votes></activity>
  5.     <activity><name>Jogging</name><votes>4</votes></activity>
  6. </list>
  7.  
for INSERT.PHP
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     header('Location:index.php');
  3.     $xmldoc = new DOMDocument();
  4.     $xmldoc->load('sample.xml');
  5.  
  6.     $newAct = $_POST['activity'];
  7.  
  8. $xmldoc->formatOutput = true;
  9.  
  10.     $root = $xmldoc->firstChild;
  11.     $newActivity = $xmldoc->createElement('activity');
  12.     $root->appendChild($newActivity);
  13.  
  14.     $newActName = $xmldoc->createElement('name');
  15.     $newActText = $xmldoc->createTextNode($newAct);
  16.     $newActName->appendChild($newActText);
  17.     $newActivity->appendChild($newActName);
  18.  
  19.     $newActVote = $xmldoc->createElement('votes');
  20.     $newActVoteCount = $xmldoc->createTextNode('1');
  21.     $newActVote->appendChild($newActVoteCount);
  22.     $newActivity->appendChild($newActVote);
  23.  
  24.     $xmldoc->save('sample.xml');
  25. ?>
  26.  
for INDEX.PHP
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>test</title></head>
  3. </head>
  4.  
  5. <form name="input" action="insert.php" method="post">
  6.     insert activity:
  7.     <input type="text" name="activity"/>
  8.     <input type="submit" value="send"/>
  9. </form>
  10.  
  11. <?php
  12.     $xmldoc = new DOMDocument();
  13.     $xmldoc->load("sample.xml", LIBXML_NOBLANKS);
  14.  
  15. $activitylist = $xmldoc->getElementsByTagName( "activity" );
  16. foreach( $activitylist as $activity )
  17. {
  18.   $actname = $activity->getElementsByTagName( "name" );
  19.   $name = $actname->item(0)->nodeValue;
  20.  
  21.   $votecount = $activity->getElementsByTagName( "votes" );
  22.   $votes = $votecount->item(0)->nodeValue;
  23.  
  24.   echo "<b>$name ----- $votes votes</b><br>";
  25. }
  26. ?>
  27. </body>
  28. </html>
  29.  
please help! I am losing my mind here!!
Dec 2 '11 #1
Share this Question
Share on Google+
1 Reply


P: 25
here's what ends up being displayed in index.php.. the last line with
"----- 1 votes" is the redundant stuff .. no idea how it got there!!

swimming ----- 2 votes
running ----- 3 votes
Jogging ----- 4 votes
swimming ----- 1 votes
----- 1 votes
Dec 2 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.