本文介绍了从HTML表单创建XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我得到它的工作,现在唯一的问题是,当一个新的提交被添加它覆盖了以前的条目。我需要它将最新的提交内容添加到XML文件中,而不是过度使用它并将其存储X时间。

这里是工作的php脚本,用于创建xml文件并从HTML表单获取数据并将其放入XML文件中。


$ b $

 <?php 

if(isset($ _ POST ['lsr-submit']) )
{
header('Location:http://www.mesquiteweather.net/wxmesqLSR.php');
}

$ str ='<?xml version =1.0encoding =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');

?>

这是它创建的xml文件。

p>

解决方案

这就是我想出来的,并且运作良好,并经过测试。注意:但是如果文件( file.xml )不存在,它会抛出一个错误,所以如果你想通过 CRON 或其他方法 (你)自动删除旧文件提到:......并将其存储X次。) ,您必须想出一种方法来制作预构建的结构化文件,其中至少包含一组



例如:

 <?xml version =1.0encoding =UTF-8?> 
<条目>
<报告>
< timestamp> 2013年5月31日,上午11:56< / timestamp>
< fname> Fred< / fname>
< lname> Fletcher< / lname>
< location>加拿大< / location>
<报告>风损< / report>
< description>风今天狂风暴雨!< / description>
< / reports>
< / entries>

这样做相对容易,我之前用如果文件存在...



以下是我的工作代码:

 <?php 

//加拿大Fred Fletcher的脚本。

$ fname = $ _POST ['firstname'];
$ lname = $ _POST ['lastname'];
$ location = $ _POST ['location'];
$ report = $ _POST ['report'];
$ description = $ _POST ['desc'];

$ xml =新的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');

回显数据已写入。

?>

脚本中的插件作为评论会很好,加拿大Fred Fletcher的脚本。 (眨眼)



让我知道这是如何解决你的。


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.

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.

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

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.

HTML FORM

解决方案

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.

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....

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.

这篇关于从HTML表单创建XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:54