2

have a form that a name has been entered and need to add it to xml file.

index.html

<form name="form" action="insert.php" method="post"> 

<label for="name">Name:</label> <br />  
<input type="text" name="name" id="name" /> <br />

<button type="submit" id="button">Submit</button> 
<br />
<span id="validate"></span>
</form> 

insert.php

    header('Location:index.php'); 

$xmldoc = new DOMDocument(); 
$xmldoc->load('recentUploads.xml'); 

$Name = $_POST['name']; 

$root = $xmldoc->firstChild;

$fileName = $xmldoc->createElement('name'); 

$root->appendChild($fileName); 

$newText1 = $xmldoc->createTextNode($Name);

$fileName->appendChild($newText1); 

$xmldoc->save('recentUploads.xml'); 

but i can not add anything to the xml file?

Help!

1
  • 2
    You're already adding, you're just missing the final DOMDocument::save Commented Jun 29, 2011 at 14:42

3 Answers 3

1

You are placing a lot of burden on poor variable $fileName:

$fileName = $_POST['name']; 
$fileName = $xmldoc-> createElement('name'); 

On the other hand, $Name is not defined when you use it in line

$newText1 = $xmldoc->createTextNode($Name);

Me thinks these two incidents are related and one $fileName should actually be a $Name.

Sign up to request clarification or add additional context in comments.

Comments

1

http://www.php.net/manual/en/domdocument.save.php

Are you remembering to save() it back?

I don't see that in your example code...

Comments

1

Do you need to call $xmldoc->saveXML();?

1 Comment

saveXML() returns a string. I assume the poster is looking to save it back to the file, so it would be the $xmldoc->save() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.