Adding a One-Time Submission Check to ConvertForms

This guide explains how to use a custom PHP script in ConvertForms to restrict users to one submission per form. The script checks if a user has already submitted the form and displays a message if they attempt to resubmit.

What Does This Script Do?

  • Prevents duplicate submissions: Blocks users from submitting the same form multiple times.
  • Form must be set for registered users only!
  • User-specific check: Uses the logged-in user’s ID (user_id) to track submissions.
  • Database validation: Queries the #__convertforms_conversions table to verify existing submissions.
  • Feedback message: Displays a warning if the user tries to resubmit.

Step-by-Step Setup

Step 1: Access the Form’s PHP Settings

  1. Log in to your Joomla administrator panel.
  2. Navigate to Components → ConvertForms → Forms.
  3. Open the form you want to modify.

Step 2: Add the Script to the Form Display Event

  1. Go to the PHP tab in the form editor.
  2. Locate the Form Display event section.
  3. Paste the provided PHP script into the editor (remove the PHP tags):
<?php
defined('_JEXEC') or die;

$query = $db->getQuery(true)
    ->select('*')
    ->from($db->quoteName('#__convertforms_conversions'))
    ->where($db->quoteName('user_id') . ' = ' . (int) $user->id)
    ->where($db->quoteName('form_id') . ' = ' . (int) $form['id']);
$db->setQuery($query);
$db->execute();

if ($db->getNumRows()) {
    $formLayout = '<div class="alert alert-warning">You have already submitted this form. Only one submission is allowed.</div>';
    return false;
}
?>

Step 3: Save and Test

  1. Save the form settings.
  2. Test the form as a logged-in user:
    • Submit the form once.
    • Try to access the form again; a warning message should appear.

How the Code Works

  1. Security Check: defined('_JEXEC') or die; prevents direct access to the script.
  2. Database Query:
    • Checks the #__convertforms_conversions table for submissions matching the current user_id and form_id.
  3. Conditional Logic:
    • If a submission exists, a warning message is displayed, and the form is hidden (return false).

Important Notes

  • User Authentication Required: This script only works for logged-in users, so your form must be set to registered users only.
  • Customisation: Adjust the message text in $formLayout to suit your needs.

Frequently Asked Questions (FAQ)

Q: Can I use this script for guest users (non-logged-in visitors)?

A: No. The script uses $user->id, which is only available for logged-in users. For guest submissions, you’d need to track submissions via IP address or another identifier.

Q: How do I disable this restriction temporarily?

A: Remove or comment out the script in the onFormDisplay event.

Q: Will this affect other forms?

A: No. The script checks the specific form_id, so it only applies to the form where it’s added.

Thanks to Tassos for the original script.