使用php将数据添加到xml

时间:2011-06-29 14:38:36

标签: php xml forms

有一个已输入名称的表单,需要将其添加到xml文件中。

的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'); 

但我无法在xml文件中添加任何内容?

帮助!

3 个答案:

答案 0 :(得分:1)

你给不良变量$fileName带来了很多负担:

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

另一方面,在行

中使用$Name时未定义$newText1 = $xmldoc->createTextNode($Name);
$fileName

我认为这两起事件是相关的,一个$Name实际上应该是{{1}}。

答案 1 :(得分:1)

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

你还记得保存()吗?

我在你的示例代码中没有看到...

答案 2 :(得分:1)

您需要拨打$ xmldoc-&gt; saveXML();?