2

I have an RSForm which has the following fields:

  • Full Name
  • Email Address

I only want to capture new data, how can I use the $_POST script to check if the email address submitted is already in the database?

According to the RSForms documentation I can do this using

$_POST['form']['email_address']

but I'm not really sure what code to use.

2
  • I believe you will need to create a function and database query to pull the email addresses from the database and match them against the one submitted from your form. The code you provided in your question will only display the email address submitted
    – Lodder
    Jan 20, 2015 at 16:57

2 Answers 2

5

You need to enter the following into the 2nd textarea in the form properties tab under "PHP Scripts":

$formId = 0; // CHANGE THIS VALUE to your form ID

$db = JFactory::getDbo();
$query = $db->getQuery();

$query
    ->select("*")
    ->from("__rsform_submission_values")
    ->where("FormId = " . $db->q($formId))
    ->where("FieldValue = " . $db->q($_POST['form']['email_address'] ));

$db->setQuery($query);
$db->execute();

$num_rows = $db->getNumRows();

// We found this email in the submissions
if ($num_rows > 0) {
    $invalid[] = RSresolveComponentName('email_address', $formId);
    echo RSshowForm($formId, $_POST['form'], $invalid);
}

You could also create a new validation rule by adding this function to this file: components/com_rsform/controller/validation.php, which can be selected by editing the email_address field and selecting "unique" as the Validation Rule.

function unique($param, $extra=null)
{        
    $db = JFactory::getDBO();
    $param = $db->getEscaped($param);
    $db->setQuery("SELECT * FROM #__rsform_submission_values WHERE `FieldName`='email_address' AND `FieldValue`='".$param."'");
    $db->query();
    $invalid = $db->getNumRows();
    
    if ($invalid)
        return false;

    return true;
}

There is a little more documentation on custom validations on this page. https://www.rsjoomla.com/support/documentation/rsform-pro/customizing-rsform-pro/custom-validation-rules.html

This thread might also prove helpful: https://www.rsjoomla.com/forum/37-rsform-pro/9162-setup-form-so-each-user-can-only-submit-once-.html

EDIT:

This answer is old and most likely obsolete, I no longer do Joomla development.

4
  • @Rusty is this answer "acceptable"? Feb 2, 2019 at 12:33
  • @Helge you have some errors that come up. The table name is missing # in the from part of the query. The RSresolveComponentName and RSshowForm functions throw errors. Though it's close enough to the final version required, it's not just a cut and paste. Oct 28, 2022 at 5:32
  • As validation.php gets overridden each time RSForms gets updated, follow the custom-validation-rules.html post to put it where it won't get overridden. Oct 28, 2022 at 6:04
  • is there any way to return custom error message on php script section instead of invalid error message for especial field?
    – Reza Amya
    Dec 27, 2022 at 16:16
1

Some things have changed in later versions of Joomla and RSForms over the past 6 years. As such @Helge Sverre's code is close but needs some modifications to work now.

  • The "Script called on form display" checks to see if the error status is true, and if it is, it displays an error message. You can customise that to your liking.
  • The "Script called on form process" is where the validation takes place. It loads the existing values for the email field from the database, then checks for a match. If there is a match, then it sets the error status to true and the validation status to false.
  • This combination then reloads the form, with the validation = false stopping the submission, and the error code triggering the error message to show.

Script called on form display

if  ($_POST['form']['error'] == true) {
    // Modify message 
    $formLayout = "<div class='alert'>
                      <h3>You have already submitted this form using this email address.</h3>
                   </div>" . $formLayout;
}

Script called on form process

$formId = 0; // Change this value to your form ID
$_POST['form']['error'] = false;

$db = JFactory::getDbo();
$query = $db->getQuery(true);
    
$query
    ->select(array('*'))
    ->from($db->quoteName('#__rsform_submission_values'))
    ->where($db->quoteName('FormId') . ' = '. $db->quote($formId))
    ->where($db->quoteName("FieldValue") . " = " . $db->quote($_POST['form']['email'] )
);
// Check fieldname 'email' above is an exact case match for the field you are validating
    
$db->setQuery($query);
$result = $db->execute();
    
$num_rows = $db->getNumRows();
    
// We found this email in the submissions
if ($num_rows > 0) {
   // Invalidate the submit button, as it doesn't have a validation message
   $invalid[] = RSFormProHelper::getComponentId("Submit");
   // Set a variable in the $_POST['form'] array to flag error
   $_POST['form']['error'] = true;
}

In the code examples above, there's some critical things you need to update to specifically match what is in your form.

  • Change the FormId value from 0 to match your form ID.
  • Check email is the name of the field being validated (you might have 'email_address', 'Email' or 'Email Address' as variations that would break the code).
  • Check Submit is the name of your submit button as that's the field this is invalidating, so needs to have a match to load in the $invalid array correctly.
  • In the use case that saw this be rewritten, we emptied the Error Message for the form.
1
  • I did try to find if I could programatically modify the form Error Message in RSForms, but did not find a solution. If you do know how to do that, then it may be possible to pass that in at the validation stage, thus using the rest of the normal processing. As you're checking for matching emails, the email validation messages are not used, as typically you would also be validating the email address in addition to the duplication. So it's not as refined as it could be, but this version I confirm works for J3.10.11, J4.2.3 and RSForms Pro 3.0.15 Oct 28, 2022 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.