Php form to XML file - working but features needed

Hi all, looks like a great site/forum here :slight_smile:

I’ve got a php form on my site that i’ve configured to email me plus update a MySQL database with the details.
The issue I now have is that I need the form contents to be added to individual XML files.

I’ve got the form to make a file with the contents, but now have two hurdles to overcome:

  1. To add the current date to one of the elements
  2. To create an individual xml file each time the form is submitted (at present it just overwrites it)

Here’s the code i’m currently plugging into a professionally written form (quform):

$doc = new DOMDocument(‘1.0’, ‘utf-8’);
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement(‘srs’);
$root = $doc->appendChild($root);

// Affiliate Code
$affcode = $doc->createElement(‘affiliate_code’);
$affcode = $root->appendChild($affcode);
$text = $doc->createTextNode(‘000000’);
$text = $affcode->appendChild($text);

// Batch ID
$batchid = $doc->createElement(‘batch_id’);
$batchid = $root->appendChild($batchid);
$text = $doc->createTextNode(‘mybatchid’);
$text = $batchid->appendChild($text);

$sr = $doc->createElement(“sr”);
$sr = $root->appendChild($sr);

$data = $doc->createElement(“data”);
$data = $sr->appendChild($data);

// Data ID
$dataid = $doc->createElement(‘id’);
$dataid = $data->appendChild($dataid);
$text = $doc->createTextNode(‘BQ00001’);
$text = $dataid->appendChild($text);

$datacat = $doc->createElement(“category”);
$datacat = $data->appendChild($datacat);

// Data Date
$datadate = $doc->createElement(‘date’);
$datadate = $data->appendChild($datadate);
$text = $doc->createTextNode(‘2014-01-12’);
$text = $datadate->appendChild($text);

$doc->save(“testoutput.xml”);

Thanks for any guidance… my knowledge is very basic indeed (almost non-existent).

Regards

Tim

Adding a date would just mean using a php date function such as

[php]$text = $doc->createTextNode(date(“Y-m-d”));[/php]

And then you need to specify a different file name for each document inplace of “testoutput.xml”

what you want to name the document depends on you and what it should hold (to give it any real value). But, to make it unique you could use a portion of a timestamp combined with some other element.

As a unique file name you could consider using uuid v4, here’s a simple php function for it

[php]<?php

function guidv4() {
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0010
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf(’%s%s-%s-%s-%s-%s%s%s’, str_split(bin2hex($data), 4));
}

echo guidv4(); // 3e396708-f041-46b1-b560-635fe4a3be77[/php]

Thanks for your help.

I thought I could rename the files and include a timestamp of some sort.

Is there a standard procedure for making files like this? For instance filename_1601_115039.xml ?

i.e. filename_day/month_hour/min/sec.xml ?

Kind regards

Tim

Thanks for all your help… i’ve got it working perfectly! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service