0

I have been searching here for a solution and found the script below to save a form submission as XML. It appends the form data to an existing XML file.

I need to be able save each form submission as a separate XML file. The name of the file can just be a timestamp. I changed the script a little because instead of $_REQUEST it was using $_POST.

<?php

// Script by Fred Fletcher, Canada.

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('file.xml');

$element = $xml->getElementsByTagName('Lead')->item(0);

$timestamp = $element->getElementsByTagName('timestamp')->item(0);
$name = $element->getElementsByTagName('Contact')->item(0);
$email = $element->getElementsByTagName('email')->item(0);
$message = $element->getElementsByTagName('message')->item(0);

$newItem = $xml->createElement('Lead');

$newItem->appendChild($xml->createElement('timestamp', date("F j, Y, g:i a",time())));;

$newItem->appendChild($xml->createElement('Contact', $_REQUEST['name']));
$newItem->appendChild($xml->createElement('email', $_REQUEST['email']));
$newItem->appendChild($xml->createElement('message', $_REQUEST['message']));

$xml->getElementsByTagName('entries')->item(0)->appendChild($newItem);

$xml->save('file.xml');

echo "Data has been written.";

?>
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
senojeel
  • 91
  • 1
  • 1
  • 12

1 Answers1

1

Try this solution, it save the data to the file which name include timestamp and microtime (to prevent file rewrite if to requests come in same second:

$xml = new DOMDocument('1.0', 'utf-8');

$lead = $xml->createElement('lead');
$lead->appendChild($xml->createElement('timestamp', date('d.m.Y H:i:s')));
$lead->appendChild($xml->createElement('name', $_REQUEST['name']));
$lead->appendChild($xml->createElement('email', $_REQUEST['email']));
$lead->appendChild($xml->createElement('message', $_REQUEST['message']));

$xml->appendChild($lead);
$xml->formatOutput = true;
$xml->save(microtime(true).'.xml');
Jekys
  • 313
  • 1
  • 8