tags:

views:

7495

answers:

9

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.

+2  A: 

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';
Owen
the issue was actually something as silly as file permission... im such a fool. the missing = is a typo, so that was not a problem. thanks a lot
David McDavidson
ha it happens :)
Owen
A: 

Hi man, your code is very cool. It works well but I have some problems to ask you in my work I use your code in my job but my xml document has this:

<?xml-stylesheet type="text/xsl" href="sample.xsl" ?>

...to connect with XSLT. Therefore, it can not create any element. Could you answer, How to create Element?

Ps this is error "Fatal error: Call to a member function appendChild() on a non-object in C:\wamp\www\quiz3\Noname5.php on line 34"

I am guessing the problem is the object you are using to call appendChild() is not an object at all. Maybe you forgot to instantiate the object you're trying to use, can't say much without looking at your code. Either way, i think it would be best if you made a proper question at the frontpage.
David McDavidson
+1  A: 

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

Change this line to

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

done, thanks for pointing it out
David McDavidson
+1  A: 

Hi!

Actually you made mistakes in two places.

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

These lines should be

Try now, it should work, Hop this would make some sense

i just fixed the typo. took me long enough! thanks
David McDavidson
A: 

Hi man, I think I know what is the problem with your code. You should not write like that: The right code I is:

A: 

hi, this is the code i work for me.

index.php

test

load('sample.xml', LIBXML_NOBLANKS);

$activities = $xmldoc->firstChild->firstChild;
if($activities!=null){
    while($activities!=null){
        echo $activities->textContent.'<br/>';
        $activities = $activities->nextSibling;
    }
}

?>

insert activity:

insert.php

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

swimming running

wicho
A: 

Final Solution

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');
?>
Amdad Hossain
A: 

Very Good.........

budi