【问题标题】:Trying to display XML data within an HTML form value尝试在 HTML 表单值中显示 XML 数据
【发布时间】: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 文件的创建速度。最后一次事故的日期就是一个典型的例子。如果自上次日期以来未发生任何事故,则用户没有理由输入该信息。

标签: php html xml


【解决方案1】:

这有点像在黑暗中拍摄.....

如果您将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 数据

EDIT2

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

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

$MOTD = getXMLMessage();
echo $MOTD;

EDIT3

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

<?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/>

【讨论】:

  • 这是一个有趣的想法,但它会访问workingbiblio.php,然后访问XML 文件吗?我不得不承认我是 PHP 和 XML 的新手,所以我也想了解其中的逻辑。
  • 刚刚尝试了您的建议,将 1workinglavin.html 更改为 PHP 页面,并且回显未将 XML 数据显示到文本框中。
  • 感谢您的帮助,希望这段代码能说明您对您要告诉我的内容有所了解。 MOTD;回声$MoTd; } ?>
  • 所以这就是它的形式?输入每日消息:
猜你喜欢
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 2021-12-25
相关资源
最近更新 更多