1

我正在尝试使用 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>
4

1 回答 1

0

这是在黑暗中的一个小镜头......

如果您制作1workinglavin.html一个 PHP 页面 ( 1workinglavin.php),您可以使用您的表单执行此操作

Label: <input name="field_6" id="field_6" type="text" value="<?php echo $field_6 ?>"/>

编辑

简单的说:

创建用于访问 XML 的函数,使它们可重用。我不知道你的 xml 的构成,所以在这里帮不上什么忙。

function getXmlMessage2()
{
  // Place code your here  getting xml data
}

接着:

  • 使用在所有页面上包含您的函数文件require_once
  • 然后你可以getXmlMessage2()在任何地方做
  • 这将阻止您一次又一次地编写相同的代码来获取您的 XML 数据

编辑2

 <?php 
 function getXMLMessage() { 
   $file = "workingXML.xml"; 
   $xml = simplexml_load_file($file); 
   $MoTd = $xml->MOTD; 
   return $MoTd; 
 } ?>

这个函数将返回 MOTD,而不是当场打印出来。然后,您可以像这样从任何文件中调用它;

$MOTD = getXMLMessage();
echo $MOTD;

编辑3

是的,返回变量很有用,因为如果需要,您可以进一步操作它。但是你也可以像这样立即回应它。

<?php 
// Be sure to include your functions file
  require_once("functions.php");
?> 
Enter Message Of The Day: 
<input name="mo_td" id="mo_td" type="text" value ="<?php echo getXMLMessage(); ?>"/>
<br/>
于 2013-07-26T18:50:51.017 回答