Jump to content

Creating XML file from HTML form


Texan78

Recommended Posts

This might be confusing so I am going to do my best at describing this.

 

I have a simple HTML form that I am trying to create an XML file from the data. Here is the link to the form.

 

http://www.mesquiteweather.net/wxmesqLSR-report.php

 

This is the php script I am using to create the XML which works if you don't redirect after the form has been submitted but, I need it to redirect so you can view the data. It populates with the data if you don't redirect. Problem is it doesn't show on the page it needs to be viewed on. Also once  data has been entered it deletes the previous data.

<?php
if (isset($_POST['lsr-submit']))
    {
        header('Location: wxmesqLSR.php');
    }
header('Content-type: text/xml');//Tell the browser it's an XML document
ob_start();
echo '<?xml version="1.0"?'.'>'."\n";//We have to break up the XML closing tag because [?]> is the closing tag for PHP
echo "<entry>
 <reports>
   <fname>{$_POST['firstname']}</fname>
   <lname>{$_POST['lastname']}</lname>
   <location>{$_POST['location']}</location>
   <report>{$_POST['report']}</report>
   <description>{$_POST['desc']}</description>
 </reports>
</entry>";
?>

What would be a good way to achieve what I am trying to do? I just need people to be able to enter info in the form and store it in a XML file and retain it for X amount of time. If I manually create a XML file the data displays fine so I know it is reading the XML file.

 

-Thanks

Link to comment
Share on other sites

Ok I have been doing some more research and this is what I am testing now which creates a XML file like I need and it's formatted properly. Issue now is how do I get the data from the submission of the form into this XML?

<?php

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

function createFile($xml_file)
{
    // create new dom document
    $xml = new DOMDocument();

    // these lines would create a nicely indented XML file
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;

    // create a root element, and add it to DOM
    addRoot($xml);

    // add more elements to xml file
    $reports = $xml->createElement("reports");

    // add this element to the root
    $xml->documentElement->appendChild($reports);

    // create elements
    $fname = $xml->createElement("fname");
    $lname = $xml->createElement("lname");

    // append these elements to friend
    $reports->appendChild($fname);
    $reports->appendChild($lname);

    // save dom document to an xml file
    $xml->save($xml_file);
}

function addRoot(&$xml)
{
    $xml->appendChild($xml->createElement("entry"));
}

// call xml function
createFile('out.xml');

?>

Here is the form...

<form name="lsrReports" action="xml/test.php" method="post">
<table width="50%" align="center" cellpadding="2" cellspacing="0">
  <tr>
    <td> First name:</td><td> <input type="text" name="firstname"></td>
    <td> Last name:</td><td>  <input type="text" name="lastname"></td>
  </tr>
  <tr>
    <td> Location:</td><td> <input type="text" name="location"></td>
    <td> Report:</td><td> <select name="report">
                             <option value="Wind Damage" selected>Wind Damage</option>
                             <option value="Hail">Hail</option>
                             <option value="Flooding">Flooding</option>
                             <option value="Power Outage">Power Outage</option>
                             <option value="General">General</option>
                          </select>
    </td>
  </tr>
  <tr>
     <td> Description: </td><td colspan="4"> <textarea rows="5" cols="65" name="desc" onfocus="this.value=''">Enter report description</textarea></td>
  </tr>
  <tr>
     <td colspan="4" style="text-align:center;"><input type="submit" name="lsr-submit" value="Submit"></td>
  </tr>
</table>
</form>

Now the HTML form is suppose to submit to the XML file then display the output to this page via the XML file. I have the page to display the XML set up, that's not the problem. I just need help with getting the info from the Form to the XML file.

 

http://www.mesquiteweather.net/wxmesqLSR.php

 

Here is the test XML file that is created from the script that the form is submitting too.

 

http://www.mesquiteweather.net/xml/out.xml

 

 

-Thanks!

Edited by Texan78
Link to comment
Share on other sites

Ok, I got it working with one small exception. 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.

 

http://www.mesquiteweather.net/xml/test2.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.

 

http://www.mesquiteweather.net/wxmesqLSR-report.php

 

So how do I add to the XML without it overwriting the last one and keep them for X amount of time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.