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
for INSERT.PHP
for INDEX.PHP
please help! I am losing my mind here!!
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
Code:
<?xml version="1.0"?>
<list>
<activity><name>swimming</name><votes>2</votes></activity>
<activity><name>running</name><votes>3</votes></activity>
<activity><name>Jogging</name><votes>4</votes></activity>
</list>
Code:
<?php
header('Location:index.php');
$xmldoc = new DOMDocument();
$xmldoc->load('sample.xml');
$newAct = $_POST['activity'];
$xmldoc->formatOutput = true;
$root = $xmldoc->firstChild;
$newActivity = $xmldoc->createElement('activity');
$root->appendChild($newActivity);
$newActName = $xmldoc->createElement('name');
$newActText = $xmldoc->createTextNode($newAct);
$newActName->appendChild($newActText);
$newActivity->appendChild($newActName);
$newActVote = $xmldoc->createElement('votes');
$newActVoteCount = $xmldoc->createTextNode('1');
$newActVote->appendChild($newActVoteCount);
$newActivity->appendChild($newActVote);
$xmldoc->save('sample.xml');
?>
Code:
<html>
<head><title>test</title></head>
</head>
<form name="input" action="insert.php" method="post">
insert activity:
<input type="text" name="activity"/>
<input type="submit" value="send"/>
</form>
<?php
$xmldoc = new DOMDocument();
$xmldoc->load("sample.xml", LIBXML_NOBLANKS);
$activitylist = $xmldoc->getElementsByTagName( "activity" );
foreach( $activitylist as $activity )
{
$actname = $activity->getElementsByTagName( "name" );
$name = $actname->item(0)->nodeValue;
$votecount = $activity->getElementsByTagName( "votes" );
$votes = $votecount->item(0)->nodeValue;
echo "<b>$name ----- $votes votes</b><br>";
}
?>
</body>
</html>
Comment