Forums

How to restrict registered user to submit only one form ?

eddddd 20 Feb, 2012
HI guys , I'm almost finish my form , just one last thing i want to add . i want to restrict registered or logged in user to submit only one form . How to do it ?

I'm thinking to add a hidden field as an indicator that this user has submitted a form and before the form is loaded chronoform will check this field if the field is 1 then a message will display .

sorry guys , I've lost the ability to do coding 😟
GreyHead 20 Feb, 2012
Hi eddddd,

Add a DB Record Loader action at the beginning of the On Load event and use that to check if there is already a record saved for this user.

Bob
enmi 27 Nov, 2012
Hi, could i get some examples of how to do this? I was thinking of writing a PHP code snippet that checks on submit that values from the form aren't in the database, seeing as this solution uses "builtin" functionality it might be a better way to go. The DB Record Loader only gets data from the table right? it cant be used to verify that there's only one record in the database with the unique e-mail for example.
GreyHead 28 Nov, 2012
Hi enmi,

If you are OK with PHP and MYSQL then I'd use a Custom Code action to run the check. If the user is logged in then you can do this when the form is loaded, if not then they need to enter their email and you can do the check after the form is submitted (or using Ajax in-line if you prefer).

This is the kind of code to use in a Custom ServerSide Validation action after the form is submitted:
<?php
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__some_table`
        WHERE `email` = '$form->data['email']' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count > 0 ) {
  $form->validation_errors['email'] = "Email has already been used.";
  return false;
}
?>
You might also want to make some part of the Form HTML conditional so that it is not redisplayed if this error occurs.

Bob
enmi 28 Nov, 2012
I ended up testing the following code:
<?php
    global $_CB_framework, $mainframe;

    if ( defined( 'JPATH_ADMINISTRATOR' ) ) {
       if ( ! file_exists( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ) ) {
          echo 'CB not installed!';
          return;
       }

       include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' );
    } else {
       if ( ! file_exists( $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' ) ) {
          echo 'CB not installed!';
          return;
       }

       include_once( $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' );
    }

    $myId   =   $_CB_framework->myId();
    $user   =&   CBuser::getUserDataInstance( $myId );

$db =& JFactory::getDBO();

$sql = "SELECT COUNT(*) FROM x6ybk_chronoforms_data_platsbokning WHERE `epost` = '$user->email'  "; 
$db->setQuery($sql); 
$results = $db->loadResult();

if ( $results > 0 ) {
	echo "Denna E-post adress har redan använts, endast en anmälan per e-post adress kan göras.";
  return false;
}
?>


Currently i only output a echo that the email already has been used, the code is put in a "custom code" ON_Load. could i somehow connect it to like a show stopper, or maybe i could use a url redirect instead that redirects the user to a Error page/article
This topic is locked and no more replies can be posted.