我正在尝试编写一个程序,从联系表单中获取http post数据,然后处理表单以放入.xml格式。我可以创建一些所需的标签,但在发送表单时不会输入任何值。以下是示例表单代码:
<form action="t.php" target="_top" method="post" accept-charset="utf-8"><div style="display:none;"><input type="hidden" name="_method" value="POST"/></div>
<div class="form-field title">
<div class="input select"><label for="title">Title</label>
<select id="title">
<option value="">(Please choose)</option>
<option value="0">Mr</option>
<option value="1">Mrs</option>
<option value="2">Miss</option>
</select></div> </div>
<div class="form-field float-left">
<div class="input text"><label for="fname">First Name</label>
<input name="fname" value="" maxlength="150" type="text" id="fname"/></div>
</div>
<div class="form-field float-left ">
<div class="input text"><label for="lname">Last Name</label>
<input name="lname" value="" maxlength="150" type="text" id="lname"/>
</div>
</div>
</form>
这是要处理的php文件:
<?php
$xmldoc = new DomDocument( '1.0' );
$xmldoc->preserveWhiteSpace = false;
$xmldoc->formatOutput = true;
$title = $_POST['title'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
if( $xml = file_get_contents( 'data.xml') ) {
$xmldoc->loadXML( $xml, LIBXML_NOBLANKS );
// find the headercontent tag
$root = $xmldoc->getElementsByTagName('headercontent')->item(0);
// create the <lead> tag
$lead = $xmldoc->createElement('lead');
$numAttribute = $xmldoc->createAttribute("num");
$numAttribute->value = $productNum;
$lead->appendChild($numAttribute);
// add the lead tag before the first element in the <headercontent> tag
$root->insertBefore( $lead, $root->firstChild );
$titleElement = $xmldoc->createElement('title');
$lead->appendChild($titleElement);
$titleText = $xmldoc->createTextNode($title);
$titleElement->appendChild($titleText);
$fnameElement = $xmldoc->createElement('fname');
$lead->appendChild($fnameElement);
$fnameText = $xmldoc->createTextNode($fname);
$fnameElement->appendChild($fnameText);
$lnameElement = $xmldoc->createElement('lname');
$lead->appendChild($lnameElement);
$lnameText = $xmldoc->createTextNode($lname);
$lnameElement->appendChild($lnameText);
$xmldoc->save('data.xml');
echo "data saved.";
?>
但这是我得到的输出:
<?xml version="1.0" encoding="UTF-8"?>
<headercontent>
<lead>
<title></title>
<fname></fname>
<lname></lname>
</lead>
</headercontent>
它只是附加容器标签(需要),但没有放入值(不需要)。任何帮助将不胜感激。
干杯