Limit Form Submission For User Per Page

Although the following code comes from RSForm for Joomla, the tool I’m currently using for my forms, I believe my question may be solved with pure PHP knowledge, although my struggle isn’t getting anywhere from here.

Is there any way to limit the user to answer the form a specific number of times in specific pages where my form shows?

I have the same form being recycled in many pages and I wish the users could answer only once per page. Is that possible at all?

I got this code that already successfully limits the number of times I wish each user to be able to submit the form, but how do I only limit them if they answer the form in specific pages? The result I wish is that they may answer the same form only once per page where they find the form.

[php]// Define the maximum number of submissions.
$max = 1;

// 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.’;
}[/php]

These forms will always be on K2 Item pages and I figured that I can get the item ID with this code:

[php]$K2Itemid = JRequest::getInt(‘id’);[/php]

I think I can use this as the condition that I’m missing. I could limit the submission with the K2 ID as a different page. May someone help me apply this? How would I insert this condition?

Sponsor our Newsletter | Privacy Policy | Terms of Service