Tassos Marinos Tassos Marinos Joomla Extensions

How to Automatically Save Each Submission to a JSON file

Published in Convert Forms
Updated 05 Nov, 2020
Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Would you like to automatically save each submission into a JSON file that you can later use and monitor your submissions? This is rather useful if you want to import the data into 3rd-party software to edit or even print them. The following PHP snippet will allow you to export each submission into a file.

Setup

To save the submissions into a file, copy the code shown below and place it into the PHP Scripts -> After Form Submission area of your form.

// Enter the file name where your submissions will be saved.
$file_name = 'submissions.txt';

// Do not edit below
$file = JPATH_SITE . '/' . $file_name;

$data = JFile::exists($file) ? json_decode(file_get_contents($file), true) : [];

if (!is_array($data))
{
    return;
}

// Set the data that will be saved
$save_data = [
	'ID'    => $submission->id,
	'Date'  => $submission->created,
	'Name'  => $submission->params['name'],
	'Email' => $submission->params['email']
];

array_push($data, $save_data);

$data = json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

// Save the file
JFile::write($file, $data);

Note: the submitted data by default are ID, Date, Name and Email. You can save further information by manipulating the $save_data variable above.