有一个已输入名称的表单,需要将其添加到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文件中添加任何内容?
帮助!
答案 0 :(得分:1)
你给不良变量$fileName
带来了很多负担:
$fileName = $_POST['name'];
$fileName = $xmldoc-> createElement('name');
另一方面,在行
中使用$Name
时未定义$newText1 = $xmldoc->createTextNode($Name);
$fileName
我认为这两起事件是相关的,一个$Name
实际上应该是{{1}}。
答案 1 :(得分:1)
答案 2 :(得分:1)
您需要拨打$ xmldoc-&gt; saveXML();?