How to add a custom PHP script to your form

You can add custom PHP scripts to your form through the Custom PHP area.

This scripting area is only available for Kali Forms Pro users.

In this section you can control the information collected through your form using PHP scripts. You have two scripting areas available, each triggered at a different time during the submission process:

  • Before form process PHP script

    As the label mentions, the script added here will be triggered before the submission is processed. You can use this to make adjustments to the collected information before it will be stored.

  • After form process PHP script

    This script will be triggered after the submission is processed. This section can be used if you want to perform additional actions with the information collected through your form.

The submitted data can be accessed with the help of the $form_data array. You can access the value submitted in a particular form field with the help of the field name, for example:

$form_data['field-name']

Let’s take for example the Contact form example. If we want to make sure that the first and last names submitted in the fields have the first letter in uppercase, then we can use the following code in the Before form process PHP script section:

$form_data['first-name'] = ucfirst($form_data['first-name']);
$form_data['last-name'] = ucfirst($form_data['last-name']);

For the second scripting area, let’s try using a script that will store the submitted data in a text file. The script should be similar to, we have included comments to help you better understand the script:

// here we will set the full path to the file that will contain the submission data
$file = '/hosting/server/path/to/your/file/fileName.txt';
 
// next we will initiate an empty $data string that will contain our submission data
$data = '';
 
// we will then loop through all of the submitted data and add it to $data. The nonce field is not relevant so we will skip it.
foreach ($form_data as $name => $value) {
if($name != "nonce") {
// when the field is a multiple selection field (eg. checkbox, dropdown) we'll have to separate the values by a comma.
if (is_array($value)) {
$value = implode(', ', $value);
}

// add the information using the syntax (field-name=field-value) to $data
$data .= "$name=$value\r\n";
}
}
 
// lastly store $data in the $file.
file_put_contents($file, $data);

You will need to set the proper path to your submission file and change the name, the rest of the script can be left as it is. When the submission is made, if you have the Save entries option enabled then the information submitted through the form should be added to the text file.

Was this article helpful to you? Yes No

Still stuck? We’re here to help.

Send us a message and we’ll get back to you as soon as we can.