Adding a counter for submissions

In this article we will describe how to create a submission counter and displaying it in the "Thank you page". Also we will show you how to limit the number of submissions based on different criteria: total number of submissions, username and IP address.

Example 1: Display the submission counter in the "Thank you message"
  1. Create a new hidden field, name it "counter";

  2. In the Scripts called on form process field from the PHP Scripts tab please add the following code(this will count the submissions and store the value in the previously defined hidden field)

 
$db = JFactory::getDbo();
$db->setQuery("SELECT COUNT(`SubmissionId`) FROM #__rsform_submissions WHERE FormId='".(int) $formId."'");
$_POST['form']['counter'] = $db->loadResult()+1;
 
Note: To display the result please edit your "Thank you message" and add the following:

  • You are submitter number: {counter:value}
Example 2: Limit the number of submissions per username
Adding the script

To achieve this, a script within the "Script called on form display" area(backend > Components > RSForm!Pro > Manage Forms > your form > Propeties > PHP Scripts) is required:

 
// Define the maximum number of submissions.
$max = 5;
 
// Get the current logged in user.
$user = JFactory::getUser();
 
// Get a database connection.
$db   = JFactory::getDbo();
$query   = $db->getQuery(true);
 
// Setup the query.
$query->select('COUNT('.$db->qn('Username').')')
    ->from($db->qn('#__rsform_submissions'))
    ->where($db->qn('FormId').'='.$db->q($formId))
    ->where($db->qn('Username').'='.$db->q($user->get('username')));
    // You can also count by User ID, just replace the above with:
    // ->where($db->qn('UserId').'='.$db->q($user->get('id')));
 
$db->setQuery($query);
$counter = $db->loadResult();
 
if ($counter >= $max){
  $formLayout = 'Sorry, you have reached the maximum number of submissions for this form.';
}
 

Important! For the script above to actually work, you will have to replace:

  • $max = 5; - the maximum number of submissions.
Example 3: Limit the number of submissions per IP address
Adding the script

To achieve this, a script within the "Script called on form display" area(backend > Components > RSForm!Pro > Manage Forms > your form > Propeties > PHP Scripts) is required:

 
// Define the maximum number of submissions.
$max = 3;
 
// Get the current IP address.
$ip = $_SERVER['REMOTE_ADDR'];
 
// Get the database connection.
$db = JFactory::getDbo();
 
// Setup the query.
$db->setQuery("SELECT COUNT(`UserIp`) FROM `#__rsform_submissions`
        WHERE `UserIp`='".$db->escape($ip)."'
        AND `FormId`='".(int) $formId."'");
$counter = $db->loadResult();
 
if ($counter >= $max){
  $formLayout = 'Sorry, you have reached the maximum number of submissions for this form.';
}
 

Important! For the script above to actually work, you will have to replace:

  • $max = 3; - the maximum number of submissions.
Example 4: Limit the number of submissions to a form

Limiting the number of submissions to a particular event can prove useful (when you are using the form as a subscription form for example). To achieve this, a script in the "Script called on form display" area is required:

 
// Define the maximum number of submissions. For this example we'll use 50.
$max = 50;
 
// Get a database connection.
$db = JFactory::getDbo();
 
// Setup the query. This query counts the number of submissions for the current form.
// $formId contains the ID of the current form.
$db->setQuery("SELECT COUNT(`SubmissionId`) FROM #__rsform_submissions WHERE `FormId`='".(int) $formId."'");
$submissions = $db->loadResult();
 
if ($submissions >= $max) {
  $formLayout = 'Sorry, no more submissions are accepted.';
}
 

The above example will limit the submissions for the current form. If the limit is reached the actual form will be replaced with a warning message.


20 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that