• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Limit form submissions per user/per day

Limit form submissions per user/per day 4 years 10 months ago #39169

  • RPPEO
  • RPPEO's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 67
  • Thank you received: 6
Had a form submitted 42 times by a user yesterday. It was over a 7 hour time window, we'd like to prevent that from happening.

I have found a few examples to limit the number of times a form can be submitted based on IP, Username, Email, and Time, however I'm not clear on how to combine a counter that checks UserId and today's date such that the MAX number of submissions per user per day is 2 (I'd like to allow 1 do over).

This does work well for a max# of submissions per UderId or Username:

// 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.';
}

How can we add a 24 HOUR rule to the counter?

Thank you in advance
The administrator has disabled public write access.

Limit form submissions per user/per day 4 years 10 months ago #39170

  • RPPEO
  • RPPEO's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 67
  • Thank you received: 6
FWIW I have found a counter that limits based on a 24 HOUR time period per user, but seems to only check if USER has submitted today.

$db = & JFactory::getDBO();
$today = date("Y-m-d");
$rsip =$_SERVER;
$db->setQuery("SELECT COUNT(`SubmissionId`) FROM #__rsform_submissions WHERE `FormId`='your_form_id_here' AND `DateSubmitted` LIKE '".$today." %' AND `UserIp` = '".$rsip."'");
$submissions = $db->loadResult();
if ( $submissions > 0)
$formLayout = 'You cannot submit again.';

If I try to make changes to either I get errors or they stop working. The example above is by UserIP which is okay if your users are all at different locations however if the form is used internally all staff will appear to have the same IP. UserId is better here, however I can't figure out how to add a second bit of logic that counts the submissions $today, such that if $submissions <= 2 you can not submit again.

Thank you again.
The administrator has disabled public write access.

Limit form submissions per user/per day 4 years 10 months ago #39171

  • adrianp
  • adrianp's Avatar
  • OFFLINE
  • RSJoomla! Official Staff
  • Posts: 631
  • Thank you received: 146
Hello,

Although it won't actually count 24 hours, you can try using the following snippet instead which basically takes into account the current date (while editing the form > Properties > PHP Scripts > Scripts called on form Display):
$currentDate = date("Y-m-d");
$max = 1; 
 
$user = JFactory::getUser();
$db = JFactory::getDbo();
$db->setQuery("SELECT COUNT(`Username`) FROM `#__rsform_submissions` WHERE `Username`=".$db->q($user->username)." AND `FormId`='".(int) $formId."' AND `DateSubmitted` LIKE '".$currentDate." %'");
$counter = $db->loadResult();
 
if ($counter >= $max){
$formLayout = '<p style="color:red;">You have reached the maximum number of submissions for today. Please try again tomorrow.</p>';
}

PS: This does assume that your users are logged-in while accessing your form.
This is not official customer support. To receive your support, submit a support ticket here.
The administrator has disabled public write access.
The following user(s) said Thank You: RPPEO

Limit form submissions per user/per day 4 years 10 months ago #39172

  • RPPEO
  • RPPEO's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 67
  • Thank you received: 6
Yes they are registered users, works like a charm.

Thank you!!
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!