尝试在HTML表单值中显示XML数据

I'm trying to create a site with HTML forms that uses PHP to create an XML file. As of right now it generates the XML file, but I would like the data to be displayed within the HTML form (I tried PHP code snippits within HTML value) once an XML file has been generated. This would display the previous information and allow editing to any of the fields. This would then be used to generate a new XML file.

HTML form to display previous XML data (to remove redundant typing such as the date of last accident) AND receive data to pass to PHP script.

PHP script takes the data input into the HTML form and generates an XML file.

saved as 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>

Saved as 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\"?>
<say> 
";

$xml_document.="<MOTD>
";
$xml_document.="$mo_td
";
$xml_document.="</MOTD>
";


$xml_document.="<field_2>
";
$xml_document.="$days
";
$xml_document.="</field_2>
";


$xml_document.="<field_3>
";
$xml_document.="$field_3
";
$xml_document.="</field_3>
";


$xml_document.="<field_4>
";
$xml_document.="$field_4
";
$xml_document.="</field_4>
";


$xml_document.="<field_5>
";
$xml_document.="$field_5
";
$xml_document.="</field_5>
";


$xml_document.="<field_6>
";
$xml_document.="$field_6
";
$xml_document.="</field_6>
";


$xml_document.="<field_7>
";
$xml_document.="$field_7
";
$xml_document.="</field_7>
";


$xml_document.="<field_8>
";
$xml_document.="$field_8
";
$xml_document.="</field_8>
";


$xml_document.="<field_9>
";
$xml_document.="$field_9
";
$xml_document.="</field_9>
";


$xml_document.="<field_10>
";
$xml_document.="$field_10
";
$xml_document.="</field_10>
";


$xml_document.="<field_11>
";
$xml_document.="$field_11
";
$xml_document.="</field_11>
";


$xml_document.="<field_12>
";
$xml_document.="$field_12
";
$xml_document.="</field_12>
";


$xml_document.="<field_13>
";
$xml_document.="$field_13
";
$xml_document.="</field_13>
";


$xml_document.="<field_14>
";
$xml_document.="$field_14
";
$xml_document.="</field_14>
";


$xml_document.="<field_15>
";
$xml_document.="$field_15
";
$xml_document.="</field_15>
";

$xml_document.="
</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>";
 }

This is saved as 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>

This is a bit of a shot in the dark.....

If you make 1workinglavin.html into a PHP page (1workinglavin.php) you can then do this with your form

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

EDIT

Simply put:

Create functions for accessing your XML, make these reusable. I do not know the make up of your xml so cannot help much here.

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

And then:

  • Include your functions file on all pages using require_once
  • You can then just do getXmlMessage2() from anywhere
  • This will stop you writing the same code again and again to get your XML data

EDIT2

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

This function will return the MOTD, rather than printing it out then and there. You can then call this from any of your files like this;

$MOTD = getXMLMessage();
echo $MOTD;

EDIT3

Yes, having variable returned is useful because you can manipulate it further if need be. But you could echo it right away too like this.

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