Forums

Limit form

jrthor2 03 Aug, 2013
I made a registration form and was wondering if it possible to limit the form to only allow the first 20 (or whatever number) registrations, then don't allow any more?

Thanks!!
GreyHead 03 Aug, 2013
Hi jrthor2,

Yes, you'd need to use some code at the beginning of the On Load event to check if there were any spaces left and then either show the form or show a 'sorry' message.

Please see this FAQ on conditional actions.

Bob
jrthor2 05 Aug, 2013
So I would need to get a count of rows from the database table for that particular form, and if it were <= to 20, then show the form, otherwise show a 'sorry' message. Can anyone help me with the coding portion of this?

Thanks!
jrthor2 05 Aug, 2013
So, is the below correct to get the total number of rows? I put this in a Custom Code Action,


<?php
// Get a db connection.
$db = JFactory::getDbo();
 
// Create a new query object.
$query = $db->getQuery(true);
 
// Select all records from the jml_chronoforms_data_wellnessoilsevent table.
$query->select(*);
$query->from('jml_chronoforms_data_wellnessoilsevent');
 
// Reset the query using our newly populated query object.
$db->setQuery($query);
 
// Load the results as a list of stdClass objects.
$rows = $db->loadObjectList();

$countrows = count($rows);
?>

How would I use $countrows to show the form or show the 'sorry' message?

Thanks
GreyHead 05 Aug, 2013
Hi jrthor2,

That's a bit more code than you need.
<?php
// Get a db connection.
$db = JFactory::getDbo();
$query = "
  SELECT COUNT(*) 
    FROM `jml_chronoforms_data_wellnessoilsevent` ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count > 0 ) {
  return 'event_a';
}
?>

Put this in an Event Switcher [GH] action (or, slightly amended, in a Custom Server Side Validation action) and you have the basis of your conditional form.

Bob
jrthor2 07 Aug, 2013
Would I replace "event_a" with the name of my form?

Also, would I change the below code to:

if ( $count > 20 ) {
  return 'event_a';
}

And how would i redirect to a different page (or show a different message) if there are more than 20? And if there are less, then do I need return anything to show the current form? Would this go in the "On Load" section of the wizard editor?

Thanks for your help?
GreyHead 13 Aug, 2013
Hi jrthor_2,

No, leave 'event_a' as it is. That will then run any actions you put in the On Event A box of the Event Switcher [GH] action. I think that the action Help tab explains more about how to use it.

You would normally put the Event Switcher [GH] action in the On Submit event of your first form and then use Show Form actions (followed by a Show Stopper action) to switch to a different form.

Note that you probably also need to use Data to Session + Session to Data actions to pass form data from one form to another.

Bob
This topic is locked and no more replies can be posted.