如何将xml文件的结果回显到用户输入框?

时间:2016-03-02 22:40:35

标签: php html xml

我有一个发布到xml文件的表单。我想将结果回显给表格。有什么想法吗?这是我的PHP和我的HTML?我没有缩写,所以如果有人需要帮助,这是一个有效的模式......

<?php

if (isset($_POST['lsr-submit']))
{
    header('Location: http://google.com');
}

$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);

$name = $_POST['name'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];

$hostname = $_POST['hostname'];
$domain = $_POST['domain'];
$nameserver = $_POST['nameserver'];
$ipaddr = $_POST['ipaddr'];
$netmask = $_POST['netmask'];
$gateway = $_POST['gateway'];
$ntpserver = $_POST['ntpserver'];
$tz = $_POST['tz'];


$name = htmlentities($name, ENT_COMPAT, 'UTF-8', false);


$xml->customer = "";
$xml->customer->addChild('name', $name);
$xml->customer->addChild('city', $city);
$xml->customer->addChild('state', $state);
$xml->customer->addChild('zip', $zip);
$xml->customer->addChild('email', $email);

$xml->emulator = "";
$xml->emulator->addChild('hostname', $hostname);
$xml->emulator->addChild('domain', $domain);
$xml->emulator->addChild('nameserver', $nameserver);
$xml->emulator->addChild('ipaddr', $ipaddr);
$xml->emulator->addChild('netmask', $netmask);
$xml->emulator->addChild('gateway', $gateway);
$xml->emulator->addChild('ntpserver', $ntpserver);
$xml->emulator->addChild('tz', $tz);

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('customer.dat');

?>

和html

<form name="lsrReports" action="xml/process.php" onSubmit="return  defaultagree(this)" method="post">
<table width="50%" align="center" cellpadding="2" cellspacing="0">
<tr>
<td> name:</td><td> <input type="text" name="name"  required /></td>
</tr>
</table>

</form>

我只包含了&#39;名称文字输入框。所以我想知道如何回应xml文件结果的value属性?这样我就有了一个易于编辑的表单。 :)

1 个答案:

答案 0 :(得分:0)

在您的HTML中:

<form>
    <input type="text" value="<?php echo $doc->saveXML(); ?>" />
</form>

当然,必须在该行之前定义$ doc。您可能想要使用textarea。