How to Limit Sending Forms to Users, Per Page?

Asked

Viewed 44 times

0

Please forgive me because I am very lay to PHP, but once in a while it is enough to turn me where necessary. But now I’m stuck and I’d be very grateful if you could shed some light.

I have a code that I can use in Joomla Rsform that limits the number of submissions that any user can make in a proper form. This part works smoothly and as I want, limits the submission to one form per user.

However, my question is: Since I use this same form for sending on several different pages, as I would to limit the sending of users also taking into account the page that the user is sending?

The expected end result is that each user can send one per page where they have access to the form!

I have this following code that already limits to a submission per user, I just need it to also take into account the page:

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

I appreciate anyone who can help!


EDIT:

One more piece of information. These forms will always be on a K2 item page. I found that I can by PHP identify the ID of the K2 item of the current page like this:

$K2Itemid = JRequest::getInt('id');

Could someone help me use this information to limit sending via K2 ID? Suddenly it’s a condition to add in the code.

  • It may not be the ideal solution for what you want, but because you don’t use a Contact Form Module for each page. So you have a Contact Form and another one for "Budget" for example. And instead of controlling by page you control by Module

  • @hugocsl Because this form will not be used for contact or some other simple task. It will be used to create different K2 items depending on which K2 item page the user is on. Let’s say I have a page that is a K2 item with the title: "Example 1" and another one with "Example 2" and this same form is in both. The user can send a new K2 item each just once.

  • I got it Leon, K2 is pretty cool, I used in the version of Joomla 2.5 yet rss. In that case I won’t be able to help you much then.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.