PHP Scripts

The RSForm!Pro script sections offer an increased flexibility. With the proper PHP / MySQL / Javascript knowledge you can do just about anything form related.

The PHP Scripts area has three different PHP sections, that are triggered on certain events.

Scripts called on form display

The scripts added in this area are executed just before the form is displayed. The main focus in this area is the $formLayout variable. It contains the HTML code of the form.

Example. Restrict access to the form, if the user is not registered:

    if ($user->get('id') == 0) {
        $formLayout = '<p>Sorry, you must be registered.</p>';
       }
Scripts called on form process

The scripts called on form process area are executed after the form has been submitted (regardless if it's valid or not). This area is rather useful when you need to process calculations. Any modifications to $_POST variables performed here, will be reflected in the submission entry. For example:

    $_POST['form']['total'] = $_POST['form']['field1'] + $_POST['form']['field2'];
 

Another interesting feature is that you can invalidate a field entry, directly from this scripting area. This is particularly useful when you are trying to validate a form field that directly depends on other field values. To achieve this, you will need to manipulate the $invalid RSform!Pro variable. This is basically an array that stores component ids of fields that have failed their validation.

Note: The component id is the index entry (named ComponentId) available in the rsform_components database table. At this point you are probably thinking that this is rather hard to use. The RSform!Pro development team thought of this, and created a helper function that will get the component id with one line of code: RSFormProHelper::getComponentId($name, $formId). Explanations:

  • $name: this variable should be populated with the exact name of the form field (available in the RSform!Pro Components tab). This variable is mandatory
  • $formId: the current form id. This is an optional variable.

Example. We will invalidate a form field, named "Other", if a field named "Options", a radio group for example, has a "Not in the list" value selected:

      if($_POST['form']['Options'] == 'Not in the list' && $_POST['form']['Other'] == '')
      $invalid[] = RSFormProHelper::getComponentId("Other");
Script called after form has been processed

The scripts called after form process is triggered after emails have been sent and the data has been saved into the database. This is rather useful when you are trying to redirect the form depending on some variables (such as submitted input fields). For example (lets assume that "field1" is a drop-down):

    if($_POST['form']['field1'][0] == 'Sample value here')
      $mainframe->redirect("http://google.com");
 

On a different note, you can alter the $thankYouMessage variable, which stores the configured Thank you message. The following example will check if you have entered a value in the "Email" field and then add an additional line to your already configured Thank you message based on the result.

    if($_POST['form']['Email'] != '')
       $thankYouMessage .= 'This is the email you have used in the form: '.$_POST['form']['Email'];
    else
       $thankYouMessage .= 'You did not use an email address in the form.';

56 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that

You Should Also Read

Custom PHP code HOT

Display PHP variables by default when form is shown HOT

Custom validation rules HOT

Controlling the calendar HOT

How can I prevent the user from selecting a date in the calendar field HOT