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.
Variable name | Type | Description |
---|---|---|
$formLayout | string | 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). Any modifications to $_POST variables performed here, will be reflected in the submission entry.
Variable name | Type | Description |
---|---|---|
$invalid | array | stores component ids of fields that have failed their validation. |
$isAjax | boolean variable | this helps differentiate an AJAX request. |
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 variable.
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: $invalid 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");
Example: $isAjax When using a multipage form, if you enable the "Validate when changing page" option, an AJAX call is used to perform the validation. This call also triggers any scripts you place in this scripting area and could generate inconsistencies with the desired functionality of the form. In order to avoid this you can use:
if(!$isAjax) { // .. your custom script here .. }
You can also make sure that the script is only triggered when no validation errors are encountered using:
if(!$isAjax && !$invalid) { // .. your custom script here .. }
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), control the thank you message displayed after submission and much more.
Variable name | Type | Description |
---|---|---|
$thankYouMessage | string | contains the HTML code of the Thank You Message. |
$u | string | contains the URL of the Thank You Message. |
$SubmissionId | variable | contains the id of the current submission. |
Example: Redirect(lets assume that "field1" is a drop-down):
if($_POST['form']['field1'][0] == 'Sample value here') $mainframe->redirect("http://google.com");
Example: $thankYouMessage.
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.';
Example: $u + $SubmissionId.
The following example will check if the submission id is greater than 100 and add an extra parameter to the URL.
if($SubmissionId > 100) $u .= '&extraParameter=over100'; else $u .= '&extraParameter=almostThere';
59 persons found this article helpful.