preprocessformdata (custom pre-processing function)

As site owner (or developer), you can include custom PHP on your contact page before the call to twzContact. If a function named preprocessformdata exists, twzContact will call it immediately on submission of the form, before any checks are done by twzContact.

The idea is that the preprocessformdata function can perform custom checks on the submitted data, in particular more complex checks that are not currently performed by twzContact. For example you might have two date or calendar fields on your form that require some predetermined relationship between them. preprocessformdata() will be called without parameters; it should directly interrogate the $_POST array to perform its checks.

This function should not be used for saving submitted data, because the submission may still contain errors. Use a processformdata function instead.

Return value

The function should return an array containing only fields that are incorrect or whose value should be changed. The array should be indexed by field name, where each entry is a sub-array with one or both of these indexes:

If all fields are found to be correct, the function should return an empty array.

The fields in the returned array will NOT be further checked by twzContact. If you want to change the value as entered by the user but still want twzContact to perform the normal checks on the field, you should change the value directly in the $_POST array, and not include the field in the returned array.

Example

In the following sample include text, the preprocessformdata function checks that the two date fields are at least 3 days apart:

<?php

function preprocessformdata()
    {
    $Result=array();
    
    if(trim($_POST['depart_date'])=='')
        { $Result['depart_date']['error']='<b>%Field%</b> not selected.'; }
    elseif(trim($_POST['arrival_date'])=='')
        { $Result['arrival_date']['error']='<b>%Field%</b> not selected.'; }
    else
        {
        $Date1=strtotime($_POST['arrival_date']);
        $Date2=strtotime($_POST['depart_date']);
        $Days=($Date2-$Date1)/(60*60*24);
        if($Days < 3)
            {
            $Result['arrival_date']['error']='Dates must be at least 3 days apart.';
            $Result['depart_date']['error']='';
            }
        }
    return $Result;
    }

define('INI_FILE', '../path.to.ini.file'); 
require_once('twzContact.php');
?>

Notice the empty error message for depart_date: $Result['depart_date']['error']='';. This is because although we are checking two fields, but we only want one error message if there's a problem. The empty string means there is no separate message for depart_date, but its presence will still highlight the depart_date field as being problematic.

Note also that this example assumes the date fields are of type 'calendar' (popup calendar). If the fields were of type 'date' (drop-downs for day, month, year), then there would be three separate entries in $_POST for these. For example arrival_date would have POST fields: arrival_dateDay, arrival_dateMon and arrival_dateYr.
While 'calendar' fields can be converted to Unix time simply with: strtotime($_POST['depart_date']) for 'date' fields you would use: strtotime($_POST['arrival_dateYr'].'-'.$_POST['arrival_dateMon'].'-'.$_POST['arrival_dateDay'])