我是PHP的新手。我正在尝试从HTML表单值创建XML文档。
这是PHP代码:
<?php
if (isset($_POST['lsr-submit']))
{
header('Location: http://movie1b.tk');
}
$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];
$fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
$lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
$location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
$report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
$description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);
$xml->reports = "";
$xml->reports->addChild('fname', $fname);
$xml->reports->addChild('lname', $lname);
$xml->reports->addChild('location', $location);
$xml->reports->addChild('report', $report);
$xml->reports->addChild('description', $description);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test2.xml', 'a');
?>
这是HTML代码:
<form name="lsrReports" action="test.php" method="post">
<table width="50%" align="center" cellpadding="2" cellspacing="0">
<tr>
<td> First name:</td><td> <input type="text" name="firstname"></td>
<td> Last name:</td><td> <input type="text" name="lastname"></td>
</tr>
<tr>
<td> Location:</td><td> <input type="text" name="location"></td>
<td> Report:</td><td> <select name="report">
<option value="Wind Damage" selected>Wind Damage</option>
<option value="Hail">Hail</option>
<option value="Flooding">Flooding</option>
<option value="Power Outage">Power Outage</option>
<option value="General">General</option>
</select>
</td>
</tr>
<tr>
<td> Description: </td><td colspan="4"> <textarea rows="5" cols="65" name="desc" onfocus="this.value=''">Enter report description</textarea></td>
</tr>
<tr>
<td colspan="4" style="text-align:center;"><input type="submit" name="lsr-submit" value="Submit"></td>
</tr>
</table>
</form>
每次按下提交按钮,它都会覆盖现有的XML。我想知道如何在不覆盖XML的情况下保存用户值,如果文件已经存在,我希望它在现有xml的顶部添加用户值
谢谢你们。
答案 0 :(得分:1)
不是每次都创建一个新的XML文件,而是要加载现有的XML文件。要尽可能多地重用代码,请使用SimpleXML的文件读取功能:
// This file "test.xml" is pre-populated with your base XML:
// <?xml version="1.0" encoding="UTF-8"?><entrys></entrys>
$xml = simplexml_load_file('test.xml');
我们需要对您的架构进行更改; <entrys>
应该是<entry>
个节点的类似数组的节点。正确?如果是这样,请通过创建父节点来替换当前的addChild
方法,以捕获名字,姓氏等。
$entry = $xml->addChild('entry');
$entry->addChild('fname', $fname);
$entry->addChild('lname', $lname);
$entry->addChild('location', $location);
$entry->addChild('report', $report);
$entry->addChild('description', $description);
运行两次,我们会得到类似的东西:
<?xml version="1.0" encoding="UTF-8"?>
<entrys>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
<entry>
<fname>abc</fname>
<lname>abc</lname>
<location>abc</location>
<report>abc</report>
<description>abc</description>
</entry>
</entrys>