-1

I'm trying to save the results of an HTML form to an XML file for later processing. The form has Unfortunately, it looks like I can only save one answer at a time, but I'd like to write all the answers in the XML file. Here is what I tried:

<?php
if(isset($_POST['submit']))
{
    $mileage = $_POST['form'];
    $file = fopen("data.xml","r+");
    fwrite($file,$mileage);
    fclose($file); 
    print_r(error_get_last());
}
?>

<form action= "" method="post" name="form">
<input type="hidden" value = "<?xml version='1.0' encoding='UTF-8'?>"/>
<input type="hidden" value="<miles>"/>
<span class="XMLsimpleType" onclick="selectelm(this, event);" ondblclick="selectelm(this, event);">
<label>Car: </label>
<input type="text" class="car" name="car" value="" onfocus="focusGained(this)" onchange="textChanged(this)" /></span>
<br/></span>
<br>
<span class="XMLsimpleType" onclick="selectelm(this, event);" ondblclick="selectelm(this, event);">
<span class="bike">
<label>Bike: </label>
<input type="text" class="bike" name="bike" value="" onfocus="focusGained(this)" onchange="textChanged(this)" /></span>
<br/></span>
<br>
<input type="hidden" value="</miles>"/>
<input type="submit" name="submit" value="submit"><br>
</form>

The form should create a properly formatted simple XML file that looks like this:

<?xml version='1.0' encoding='UTF-8'?>
<miles>
<car>123</car>
<bike>245</bike>
</miles>

I've spent the past four hours looking at this, and I know I can make it simpler, but first I'd like to just get it working. I can write a single form entry, but not the whole form with the XML information. What have I done wrong?

Harold Scott
  • 3
  • 1
  • 2
  • You cannot process the form the way you are trying to do it. For one, your $_POST array only contains the input elements with a name attribute (i.e. car, bike and submit in your form). The answer would be to turn your $_POST array into a valid XML structure, using SimpleXMLElement, see the link [link]https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml – lovelace Oct 07 '17 at 20:46
  • I tried to just combine all input elements into one $_Post, using periods, commas, and just spaces, and none of that worked. I'll look into the SimpleXMLElement, unless there is an easier way to combine things. – Harold Scott Oct 07 '17 at 22:15

1 Answers1

2

This should work (I have split the form and processing into two seperate files):

yourform.php (simplified):

<form action='showXML.php' method='POST'>
car <input type='text' name='car'>
<br />
bike <input type='text' name='bike'>
<input type='submit' name='submit' value='submit'>
</form>

showXML.php:

<?php
if(isset($_POST['submit']))
{
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><miles></miles>");

    $xml->addChild('car', $_POST['car']);
    $xml->addChild('bike', $_POST['bike']);

    $asXML = $xml->asXML();
    $file = fopen("data.xml","w+");
    fwrite($file,$asXML);
    fclose($file); 
    print_r(error_get_last());

    if(file_exists('./data.xml'))
    {
        $myXML = file_get_contents('./data.xml');
        $xml = new SimpleXMLElement($myXML);
        $xmlpretty = $xml->asXML();

        // pretty print the XML in browser
        header('content-type: text/xml');
        echo $xmlpretty;
    }

}
?>
lovelace
  • 1,195
  • 1
  • 7
  • 10
  • That worked. I was getting close with SimpleXMLElement, but I couldn't figure out how to use $_Post with the array. I was about to try just writing $asXML to a file, which is what you appeared to do. Excellent, job, thanks. – Harold Scott Oct 07 '17 at 22:35
  • Thanks Harold, glad I could help – lovelace Oct 09 '17 at 07:06