How to perform a redirect based on the value entered in a Webform component?

by mtin -nong   Last Updated November 23, 2017 12:07 PM

How to perform a redirect after a Webform submission, i.e. to different pages for different answers?

The scenario is like so:

  • If user enters "foo" for some Webform component, then page must be redirected to /foo.
  • If user enters "anything else but foo", then page must be redirected to /bar.
Tags : rules webform


Answers 2


you can achieve by altering the Webform.

    function hook_form_alter(&$form, &$form_state, $form_id) {
      if ($form_id == 'YOURFORMID') {
     $form['#submit'][] =='ctm_redirect';
     }
    }

    function ctm_redirect(&$form, &$form_state) {
      if(CONDITIONS) {
      $form_state['redirect'] = 'foo';
      }
      else{
        $form_state['redirect'] = 'bar';
      }
    }

You can also check Webform Rules module.

This module adds rules integration on webforms.

Did you ever want to react on webform submission using rules but couldn't find the proper event? Webform Rules makes it possible to catch webform submissions by rules and do whatever you'd like to do with it (meaning: do whatever rules let you do with it).

Smalution
Smalution
November 21, 2016 02:50 AM

As an alternative to the previous answer, you can also get this to work with the Rules module (cfr. the tag in your question), as further explained below (and assuming this question is about D7) ...

At a minimum, make sure you have the Webform Rules module enabled. Some details about it (from its project page):

This module adds rules integration on webforms.

Did you ever want to react on webform submission using rules but couldn't find the proper event?

Webform Rules makes it possible to catch webform submissions by rules and do whatever you'd like to do with it (meaning: do whatever rules let you do with it).

However, you should also have a look at feature request "Expose submission data to Rules". Here is a summary of it:

Right now, submission data is not exposed to rules except by way of token replacement or custom php actions. This patch exposes submission data in a "first-class" way.

Issue status is "Needs work" ... not an "easy" issue.

Time for a work around ...

The very same issue contains a comment with a workaround (it's genious I think, because it's so simple), here is a relevant quote from it:

You can initialize a rules string variable with the tokens. If you need integers or booleans you can subsequently convert it to whatever and then use it to load nodes or whatever.

A picture is worth more then 1000 words, checkout the screenprint included in that comment.

... and a demo

Consider this rule (in Rules export format):

{ "rules_redirect_based_on_a_component_value_of_a_submitted_webform" : {
    "LABEL" : "Redirect based on a component value of a submitted webform",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_conditional", "webform_rules" ],
    "ON" : { "webform_rules_submit" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "form-id" ], "value" : "5" } }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "[data:contact_us_target-value-raw]" },
          "PROVIDE" : { "variable_added" : { "submitted_form_component_value" : "Submitted form component value" } }
        }
      },
      { "CONDITIONAL" : [
          {
            "IF" : { "data_is" : { "data" : [ "submitted-form-component-value" ], "value" : "foo" } },
            "DO" : [ { "redirect" : { "url" : "foo" } } ]
          },
          { "ELSE" : [ { "redirect" : { "url" : "bar" } } ] }
        ]
      }
    ]
  }
}

Some details about the above rule:

  • Rules Event: After a webform has been submitted
  • Rules Condition: Form-id has value "5" (change to whatever form-id to fit your case).
  • Rules Actions:

    • Add a variable (this is the clue of it all ...), named submitted_form_component_value and with label "Submitted form component value", and with value [data:contact_us_target-value-raw] (the form component we want to use in subsequent Rules Actions).
    • Use the Conditional Rules module to perform a data comparison. In this case it checks if it is foo. If so, then perform Rules Action "Page redirect to foo", otherwise perform Rules Action "Page redirect to bar".

PS: In this example I'm using the Conditional Rules module, though you could also rework the Rules Action to performing Rules Components (in which you add the appropriate Rules Conditions), so that you don't need this extra module. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?".

Pierre.Vriens
Pierre.Vriens
November 23, 2017 11:11 AM

Related Questions






Webform Data Comparison In Rules

Updated February 19, 2016 01:03 AM