How do I reset the selections made in a conditional field

If you have a scenario that requires the selections you have made in a conditional field to be reset when the user makes a different selection, then you will require some custom scripting.

Lets take for example a radio group named selection with two options: fruits and vegetables.

Based on the user's selections different fields are shown through conditional statements. In order to clear the selections/values made for the fruits fields when the user selects vegetables you will require a simple script in Scripts called on form process:

if ($_POST['form']['selection'] == 'vegetables')
{
  $_POST['form']['textbox-field-name'] = '';
  unset($_POST['form']['checkbox-field-name']);
  $_POST['form']['dropdown-field-name'][0] = 'grapes';
}

Lets take a script one line at a time and explain:

  • The first line will check the value that is selected by the user when the form is submitted. Since the vegetables option is selected, the fields that are specific to the fruits option will be hidden from view, but still retain the values entered by the user before he changed his selection.

        if ($_POST['form']['selection'] == 'vegetables')
    
  • Removing the value from a textbox field is rather simple, you only need to overwrite the current value with a blank value.

        $_POST['form']['textbox-field-name'] = '';
    
  • Since checkbox group values are actually arrays that contain all the items selected by the user, in order to reset the value you will need to use the unset() function.

        unset($_POST['form']['checkbox-field-name']);
    
  • For dropdown fields, instead of setting a blank value, you will need to set the default value one more time.

        $_POST['form']['dropdown-field-name'][0] = 'grapes';
    

7 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that

You Should Also Read

Conditional Mappings scenario