【发布时间】:2013-07-26 17:58:57
【问题描述】:
我正在尝试使用 HTML 表单创建一个站点,该表单使用 PHP 创建 XML 文件。截至目前,它会生成 XML 文件,但我希望在生成 XML 文件后将数据显示在 HTML 表单中(我在 HTML 值中尝试了 PHP 代码片段)。这将显示以前的信息并允许编辑任何字段。这将用于生成新的 XML 文件。
HTML 表单显示以前的 XML 数据(以删除多余的输入,例如上次事故的日期)并接收数据以传递给 PHP 脚本。
PHP 脚本将数据输入到 HTML 表单中并生成一个 XML 文件。
保存为 1workinglavin.html
<html>
<head>
<title>XML Links Data</title>
</head>
<body>
<form method="POST" action="1workingbiblio.php">
<input type="hidden" name="create_xml" value="true">
Enter Message Of The Day: <input name="mo_td" id="mo_td" type="text" value ="??????CODE GOES HERE??????"/> <br/>
Enter Today's Date In "Month Day, Year Format": <input name="field_2" id="field_2" type="text"/> <br/>
Upcoming Events : <input name="field_3" id="field_3" type="text"/> <br/>
Safety: <input name="field_4" id="field_4" type="text"/> <br/>
Production: <input name="field_5" id="field_5" type="text"/> <br/>
Enter Message Of The Day: <input name="field_6" id="field_6" type="text"/> <br/>
Enter Message Of The Day: <input name="field_7" id="field_7" type="text"/> <br/>
Enter Message Of The Day: <input name="field_8" id="field_8" type="text"/> <br/>
Enter Message Of The Day: <input name="field_9" id="field_9" type="text"/> <br/>
Enter Message Of The Day: <input name="field_10" id="field_10" type="text"/> <br/>
Enter Message Of The Day: <input name="field_11" id="field_11" type="text"/> <br/>
Enter Message Of The Day: <input name="field_12" id="field_12" type="text"/> <br/>
Enter Message Of The Day: <input name="field_13" id="field_13" type="text"/> <br/>
Enter Message Of The Day: <input name="field_14" id="field_14" type="text"/> <br/>
Enter Message Of The Day: <input name="field_15" id="field_15" type="text"/> <br/>
<input type="submit" name="submit" value="Generate xml record"/><br/>
</form>
</body>
</html>
另存为 1workingbiblio.php
<?php
if(isset($_POST['create_xml'])){
/*makesthepresenceoftheinvisiblecreate_xmlformmandatoryforthescriptaction,
*andstatesthatthe'title'formcannotbeleftblank*/
echo"MARCxmlfilegenerated";
/*InthissectionofthecodeIamassigningphp
*variablesforallformfieldsinbiblform.html*/
$mo_td=$_POST['mo_td'];
$field_2=$_POST['field_2'];
$field_3=$_POST['field_3'];
$field_4=$_POST['field_4'];
$field_5=$_POST['field_5'];
$field_6=$_POST['field_6'];
$field_7=$_POST['field_7'];
$field_8=$_POST['field_8'];
$field_9=$_POST['field_9'];
$field_10=$_POST['field_10'];
$field_11=$_POST['field_11'];
$field_12=$_POST['field_12'];
$field_13=$_POST['field_13'];
$field_14=$_POST['field_14'];
$field_15=$_POST['field_15'];
///////////////////////////////////////////////////////////////////////////////////
$accident_date = $field_2;
$acc_ts = strtotime($accident_date); // Get UNIX Timestamp (number of seconds since 1/1/1970)
$today = time(); // Get the UNIX timestamp for today
$days = 0;
for ($test = $acc_ts; $test <= $today; $test += 86400) { // 86400 = number of seconds in a day
$dy = date('l',$test); // Thats a lowercase L, not an uppercase i or number 1. Gives day of week
if ($dy != 'Saturday' && $dy != 'Sunday') $days++; // if the day isn't a Saturday or Sunday count it.
}
$days ='Days since last accident: ' . $days;
////////////////////////////////////////////////////////////////////////////////////
$xml_document="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<say> \r\n";
$xml_document.="<MOTD>\r\n";
$xml_document.="$mo_td\r\n";
$xml_document.="</MOTD>\r\n";
$xml_document.="<field_2>\r\n";
$xml_document.="$days\r\n";
$xml_document.="</field_2>\r\n";
$xml_document.="<field_3>\r\n";
$xml_document.="$field_3\r\n";
$xml_document.="</field_3>\r\n";
$xml_document.="<field_4>\r\n";
$xml_document.="$field_4\r\n";
$xml_document.="</field_4>\r\n";
$xml_document.="<field_5>\r\n";
$xml_document.="$field_5\r\n";
$xml_document.="</field_5>\r\n";
$xml_document.="<field_6>\r\n";
$xml_document.="$field_6\r\n";
$xml_document.="</field_6>\r\n";
$xml_document.="<field_7>\r\n";
$xml_document.="$field_7\r\n";
$xml_document.="</field_7>\r\n";
$xml_document.="<field_8>\r\n";
$xml_document.="$field_8\r\n";
$xml_document.="</field_8>\r\n";
$xml_document.="<field_9>\r\n";
$xml_document.="$field_9\r\n";
$xml_document.="</field_9>\r\n";
$xml_document.="<field_10>\r\n";
$xml_document.="$field_10\r\n";
$xml_document.="</field_10>\r\n";
$xml_document.="<field_11>\r\n";
$xml_document.="$field_11\r\n";
$xml_document.="</field_11>\r\n";
$xml_document.="<field_12>\r\n";
$xml_document.="$field_12\r\n";
$xml_document.="</field_12>\r\n";
$xml_document.="<field_13>\r\n";
$xml_document.="$field_13\r\n";
$xml_document.="</field_13>\r\n";
$xml_document.="<field_14>\r\n";
$xml_document.="$field_14\r\n";
$xml_document.="</field_14>\r\n";
$xml_document.="<field_15>\r\n";
$xml_document.="$field_15\r\n";
$xml_document.="</field_15>\r\n";
$xml_document.="\r\n</say>";
$file="workingXML.xml";
$fp=fopen($file,'r');
$write=fwrite($fp,$xml_document);
fclose($fp);
$record_data=simplexml_load_file($file);
echo"<br>Wrotethexmlfile...<br>".$file."<br>";
}
这被保存为workingXML.xml
<?xml version="1.0" encoding="UTF-8"?>
<say>
<MOTD>
message of the say
</MOTD>
<field_2>
Days since last accident: 8
</field_2>
<field_3>
klj
</field_3>
<field_4>
hlkjh
</field_4>
<field_5>
kljh
</field_5>
<field_6>
lkh
</field_6>
<field_7>
lk
</field_7>
<field_8>
jhl
</field_8>
<field_9>
hg
</field_9>
<field_10>
oy
</field_10>
<field_11>
ufro
</field_11>
<field_12>
uy
</field_12>
<field_13>
yug
</field_13>
<field_14>
ldfaf
</field_14>
<field_15>
hbogo
</field_15>
</say>
【问题讨论】:
-
您是要阅读过去的 XML 文档,还是只是通过让表单显示以前输入的数据来加快新文档的创建速度?
-
加快新 XML 文件的创建速度。最后一次事故的日期就是一个典型的例子。如果自上次日期以来未发生任何事故,则用户没有理由输入该信息。