Log Online Form Requests in Case Something Happens with the E-mailed Results

When building data-collection forms online, clients occasionally want the information sent directly to them via e-mail. Although it's convenient, there are issues with that delivery method. Spam filters may catch the messages. Messages may be overlooked and accidentally deleted due to the sheer number of e-mails a person receives per day. Or something could just go wrong with the mail server and the message never gets sent. With all the potential ways for e-mails to get lost, it's important to backup the information collected. One backup option is to store the data into a text file.

Overview

Let's say our online form allows visitors to request printed copies of the company brochure. To fulfill requests as soon as possible, an e-mail goes to the person(s) responsible for handling requests.

For the sake of simplicity, this post isn't going to cover the process for building forms, sanitizing data, or sending e-mail. We'll just start with some variables containing a sample request.

<?php
//INITIALIZE DATA
$formRequest['name']    = 'John Smith';
$formRequest['address'] = '426 1st St., New York, NY 10453';
$formRequest['email']   = 'test@gmail.com';
?>

Saving to Text File

To backup the request information, let's create a variable to prepare the data for the text file.

<?php
//PREPARE DATA FOR LOG FILE
$forTextFile = date('Y-m-d') . '|' . $formRequest['name'] . '|' . $formRequest['address'] . '|' . $formRequest['email'];
?>

Note that the date was included in case we need to reference it down the road. Each field is also separated by the pipe character so the information can be easily imported into a spreadsheet application like Microsoft Excel.

Next, we'll open the text file and log the request. Note that the file is being opened in append mode which causes the new entry to be added to the end of the file.

<?php
//IF THE LOG FILE WAS OPENED SUCCESSFULLY, LOG THE REQUEST
if($fp = fopen('formRequestLog.txt', 'a')) {
    fwrite($fp, "$forTextFile\n");
    fclose($fp);
}
?>

Final Code

<?php
//INITIALIZE DATA
$formRequest['name']    = 'John Smith';
$formRequest['address'] = '426 1st St., New York, NY 10453';
$formRequest['email']   = 'test@gmail.com';
 
//PREPARE DATA FOR LOG FILE
$forTextFile = date('Y-m-d') . '|' . $formRequest['name'] . '|' . $formRequest['address'] . '|' . $formRequest['email'];
 
//IF THE LOG FILE WAS OPENED SUCCESSFULLY, LOG THE REQUEST
if($fp = fopen('formRequestLog.txt', 'a')) {
    fwrite($fp, "$forTextFile\n");
    fclose($fp);
}
?>

Conclusion

If you're responsible for building online data collection forms, it's important to do whatever you can for getting those requests to their final destination. Sending requests via e-mail is quick and convenient. However, things can happen along the way. Having an alternate way to access requests, like saving to a text file, will help prevent potential headaches.

Side note: additional steps could be taken with the data logging script to improve the overall effectiveness. For example, the script isn't going to be very useful if the text file can't be opened. This can be addressed by adding an "else" component to the if statement that opens the file. Within the else, you could activate an error logging script—if you have one.

<?php
if($fp = fopen('formRequestLog.txt', 'a')) {
    //log the form request
} else {
    //unable to open text file, log error
}
?>

0 Comments

There are currently no comments.

Leave a Comment