Conditional Mappings scenario

RSForm!Pro incorporates the possibility of creating various mappings within a local or external database. As these might come in help, you may also want to trigger specific mappings based on certain user selection for example. In order to do so, a PHP script will be used.


When specifying your mappings, there is no particular way of naming each or these for an internal reference. However, we will differentiate between mappings based on their order. In the picture on the right you can notice the order your mappings have (example: order 1 will be mapping 1).

Knowing which is which, an example will be provided.


Scenario

Our form has 2 mappings and a dropdown with the following setup:

Dropdown
  • Name: vehicle
  • Items:
    • car
    • plane

We want the first mapping (mapping 1) to trigger only when car was selected, while the second mapping (mapping 2) when plane was selected.


Script

The script is added by navigating to backend > Components > RSForm!Pro > Manage Forms > your form > Properties > PHP Scripts > Script called after form has been processed:

//mappings are stored within the $mappings array
for($i=0; $i<count($mappings); $i++){
//iterating through mappings

  if($_POST['form']['vehicle'][0] == 'car'){
  //if our dropdown selection is car

    if($mappings[$i]->ordering != 1){
    //check if it's not the first mapping (mapping 1) and unset others (removes)
      unset($mappings[$i]);
      //thus, only mapping nr. 1 will be triggered in this case
    }
  }else{
    if($mappings[$i]->ordering != 2){
    //similar as above, it checks the order and unset accordingly
      unset($mappings[$i]);
    }
  }
}

Other possible user selections

The above example uses a dropdown called vehicle, thus the following if statement:

if($_POST['form']['vehicle'][0] == 'car')
//since dropdowns are array, [0] would be the actual selection
  • For a radio group selection:
  •     if($_POST['form']['my-radio-group-name-here'] == 'carrot')
    
  • For a checkbox:
  •     //you should firstly check if any selection was actually made using isset
        if(isset($_POST['form']['my-checkbox-name-here'])){
          //afterwards, you can use PHP array_search to check the selection:
          if(array_search('strawberries',$_POST['form']['my-checkbox-name-here']) !== false){
          //mappings script goes here
          }
    
          //for multiple checkbox selections, you can define a set of choices that trigger the mappings
          $selections = array('strawberries','bananas');
          //$selections would be our array used to check if strawberries and bananas were selected in one go
          if(count(array_intersect($selections,$_POST['form']['my-checkbox-name-here'])) == count($selections)){
          //mappings script goes here, while it will be triggered when these exact multiple selections were made
          }
        }
    
  • When a textbox has particular value:
  •     if($_POST['form']['my-textbox-name-here'] == 'mango')
    

11 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that

You Should Also Read

How do I reset the selections made in a conditional field