Tassos Marinos Developer of Joomla Extensions

How to Ensure a Unique Value is Entered Into a Field

Published in Convert Forms
Updated 03 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 your form to collect unique values? This is rather useful when you have a newsletter form and you don't want already existing subscribers to re-submit the form. You would prefer to notify them that they have already subscribed rather than re-subscribing them. The following PHP snippet will provide a helpful message to your users when they try to re-submit the form they have already submitted.

Setup

To ensure a form field has a unique value, copy the code shown below and place it into the PHP Scripts -> Form Process area of your form.

// Enter the Field Name to search against
$unique_field_name = 'email';

// Enter the error message that will appear when the same value has already been submitted
$unique_field_error = 'A form with this value has already been submitted';
   
// Do not edit below
$unique_post_value = $post[$unique_field_name];

if (!$submissions = ConvertForms\Api::getFormSubmissions($form['id']))
{
    return;
}

foreach ($submissions as $key => $submission)
{
    if (!isset($submission->params->$unique_field_name))
    {
        continue;
    }

    $submission_unique_value = $submission->params->$unique_field_name;

    if ($unique_post_value == $submission_unique_value)
    {
        throw new Exception($unique_field_error);
    }
}