How to Increment a Count on Each Form Submission

Every submission in Convert Forms receives a unique, sequential ID that identifies it across all forms. However, sometimes you may want a counter starting from 1 for each form—useful for things like per-form reference numbers, ticket IDs, or order numbers.

This guide shows you how to add an auto-incrementing counter to your form using a simple PHP snippet. The counter is stored in a Hidden field and increases with every new submission for that specific form. You can also format the counter with leading zeros (e.g., 00001, 00002) for a consistent, professional appearance.

Setting Up the Form

  1. Open the form builder by creating or editing your existing form.
  2. Add a Hidden field (found under the Add Fields tab). Users won’t see this field, but it will store the counter value behind the scenes.
  3. After adding the field, set its name to total.

Adding the Code Snippet

Place the following PHP script into the PHP Scripts → Form Process option:

// Name of the Hidden field that will store the counter
$myFormField = 'total';

// Minimum number of digits for the counter (e.g., 5 will produce 00001, 00002, ...)
$minDigitLength = 5;

// Get all submissions for this form (with any state)
$formSubmissions = ConvertForms\Api::getFormSubmissions($form['id'], ['state' => [0,1,2]]);

// Calculate the new counter value by adding 1 to the current number of submissions
$newTotalSubmissions = count($formSubmissions) + 1;

// Pad the counter with leading zeros if needed
if ($minDigitLength > 0)
{
    $newTotalSubmissions = str_pad($newTotalSubmissions, $minDigitLength, '0', STR_PAD_LEFT);
}

// Store the counter value in the Hidden field
$post[$myFormField] = $newTotalSubmissions;

How It Works

  1. Field Configuration: The variable $myFormField specifies which form field will store the counter value (in this case, total).
  2. Setting Minimum Digits: The $minDigitLength variable defines how many total digits the number should have (default is 5). For example, with five digits, the first submission would be 00001.
  3. Getting Submissions: The script retrieves all existing submissions for your form, including those with any state (published, unpublished, or archived).
  4. Calculating the New Count: It counts the existing submissions and adds 1 to create the new incremental number.
  5. Adding Leading Zeros: If $minDigitLength is set greater than 0, the script will pad the number with leading zeros to match that length.
  6. Assigning the Value: Finally, the counter value is stored in your hidden field named total.

Testing Your Counter

  1. Submit a test submission through your form.
  2. Check the submission in Convert Forms » Submissions. You should see your incremental number stored in the Hidden field.
  3. Submit another test entry to confirm the number increases as expected.

Note: If you want to display this number in your email notifications or confirmation messages, simply add the shortcode {field.total} to any message in your form’s Notifications or Confirmations settings.

Last updated on May 16th 2025 09:05