php 从 HTML 表单创建 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16847841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating XML file from HTML form
提问by Texan78
Ok so I got it working, now the only problem is when a new submission is added it overwrites the previous entry. I need it to add the newest submission to the XML file and not over ride it and store it for X amount of time.
好的,我让它工作了,现在唯一的问题是当添加新提交时它会覆盖以前的条目。我需要它将最新的提交添加到 XML 文件中,而不是覆盖它并将其存储 X 时间。
Here is the working php script that creates the xml file and takes the data from the HTML form and puts it in the XML file.
这是创建 xml 文件并从 HTML 表单中获取数据并将其放入 XML 文件的工作 php 脚本。
<?php
if (isset($_POST['lsr-submit']))
{
header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
}
$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];
$fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
$lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
$location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
$report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
$description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);
$xml->reports = "";
$xml->reports->addChild('fname', $fname);
$xml->reports->addChild('lname', $lname);
$xml->reports->addChild('location', $location);
$xml->reports->addChild('report', $report);
$xml->reports->addChild('description', $description);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test2.xml');
?>
Here is the xml file it creates.
这是它创建的 xml 文件。
Here is the form to submit to the XML file. Submit a test submission and it takes you to the page to display but you'll notice it will overwrite the last one instead of adding to the XML file.
这是提交到 XML 文件的表单。提交测试提交,它会将您带到要显示的页面,但您会注意到它将覆盖最后一个而不是添加到 XML 文件中。
采纳答案by Funk Forty Niner
This is what I came up with and works well, and tested.
这是我想出来的,效果很好,经过测试。
NOTE: However if the file (file.xml
) does not exist, it will throw off an error, so if you figure out a way to automatically delete the old file(s) via CRON
or any other method (you mentioned: "...and store it for X amount of time."), you'll have to come up with a way to make a pre-built structured file with at least one set of entries inside it.
注意:但是,如果文件 ( file.xml
) 不存在,则会抛出错误,因此如果您想出一种通过CRON
或任何其他方法自动删除旧文件的方法(您提到:“...并存储X 时间。”),您必须想出一种方法来制作一个预构建的结构化文件,其中至少包含一组条目。
E.g.:
例如:
<?xml version="1.0" encoding="UTF-8"?>
<entries>
<reports>
<timestamp>May 31, 2013, 11:56 am</timestamp>
<fname>Fred</fname>
<lname>Fletcher</lname>
<location>Canada</location>
<report>Wind Damage</report>
<description>Winds were gusting mighty hard today!</description>
</reports>
</entries>
This is relatively easy to do, I've done it before with an if file exists...
.
这相对容易做到,我以前用if file exists...
.
Here is my working code:
这是我的工作代码:
<?php
// Script by Fred Fletcher, Canada.
$fname = $_POST['firstname'];
$lname = $_POST['lastname'];
$location = $_POST['location'];
$report = $_POST['report'];
$description = $_POST['desc'];
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('file.xml');
$element = $xml->getElementsByTagName('reports')->item(0);
$timestamp = $element->getElementsByTagName('timestamp')->item(0);
$fname = $element->getElementsByTagName('fname')->item(0);
$lname = $element->getElementsByTagName('lname')->item(0);
$location = $element->getElementsByTagName('location')->item(0);
$report = $element->getElementsByTagName('report')->item(0);
$description = $element->getElementsByTagName('description')->item(0);
$newItem = $xml->createElement('reports');
$newItem->appendChild($xml->createElement('timestamp', date("F j, Y, g:i a",time())));;
$newItem->appendChild($xml->createElement('fname', $_POST['firstname']));
$newItem->appendChild($xml->createElement('lname', $_POST['lastname']));
$newItem->appendChild($xml->createElement('location', $_POST['location']));
$newItem->appendChild($xml->createElement('report', $_POST['report']));
$newItem->appendChild($xml->createElement('description', $_POST['desc']));
$xml->getElementsByTagName('entries')->item(0)->appendChild($newItem);
$xml->save('file.xml');
echo "Data has been written.";
?>
A "plug" as a comment in the script would be nice, "Script by Fred Fletcher, Canada." (wink)
脚本中的“插件”作为注释会很好,“加拿大弗雷德弗莱彻的脚本”。(眨眼)
Let me know how this works out for you.
让我知道这对你有什么影响。
回答by Funk Forty Niner
I am not sure I got your question, but if you need to create a file that is unique for each submission then you cannot simply use echo, you must actually create a file.
我不确定我是否收到了您的问题,但是如果您需要为每次提交创建一个唯一的文件,那么您不能简单地使用 echo,您必须实际创建一个文件。
Also if after submitting you redirect to another PHP file, how can you handle the data to write into the variables?
此外,如果提交后重定向到另一个 PHP 文件,您如何处理写入变量的数据?
Example for creating a file:
创建文件的示例:
<?php // testfile.php
$fh = fopen("testfile.xml", 'w') or die("Failed to create file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($fh, $text) or die("Could not write to file");
fclose($fh);
?>