Form Redirect Based on Dropdown Field Value


Hi,

I’m looking for a script which allows me to send the user to either page A or page B, depending on the value of a dropdown field. For example:

User selects that they like Apples so they go to /thank-you/apples, or user selects they like oranges so they go to /thank-you/oranges.

Does anybody have a script handy that would do this?

Thanks!


42 replies

This is the latest code for Form Redirection Based on Dropdown Field Value  👇 👇 👇

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

Redirect Script

Replace with the ID of the form field containing the selection
Replace with the text of your selection
Replace with the URL corresponding to the selection

<script>
$("#formfieldID").on('change', function() {
switch ($(this).val()) {
case 'selection1':
window.module.lp.form.data.url = "URL1";
break;
case 'selection2':
window.module.lp.form.data.url = "URL2";
break;
}
});
</script>

 

Userlevel 5

I have seen a few other inquiries about using this script recently. So I figured I should post the update that works in this thread. Special thanks to @Hristian for making it work!

<script>
$("#gender").live('change', function() {
switch ($(this).val()) {
case 'Male':
    window.module.lp.form.data.url = "http://www.google.com";
    break;
case 'Female':
    window.module.lp.form.data.url = "http://www.bing.com";
    break;
};
});
</script>

I am trying to get this to work as well and no matter what I tried from all these article threads nothing is working. It would be great if someone who has gotten this to work can provide complete instructions from beginning to end on how to set this up. There is a lot of conflicting information in this forum on how this should be done.

Userlevel 5

Hey @MDesign this is the latest edition that has worked. Have you tried to mimic this one?

Also, could you share your page URL so we could take a look further?

Userlevel 7
Badge +3

Hi @davetran01,

Add jQuery (1.4.2) to your page and it should work. Just make sure the jQuery fires before the redirect script.

Best,
Hristian

A bit different from the topic, here’s a script to redirect a form submission based on a field value:

document.getElementById(‘lp-pom-button-309’).onclick = function() {
var myInput = document.getElementById(‘number_of_orders’).value
if( myInput < 10) {
window.module.lp.form.data.url = “https://url2/”;

}
else {
window.module.lp.form.data.url = “https://url1/”;
}
};

Hope that it helps someone. I am sure you could edit it to make it work with a dropdown.

Thank you!

That’s sorted now thank you. It must have been clashing with some of the other JS we had on there.

Hi @Matt_Dimock,

Not exactly sure where your issue lies, but some suggestions to help you on your way.

Perhaps there is more to this code than I can see, but you seem to be using jQuery for no reason - if it is required check that it is being loaded in the head of the page.

You’re relying on hooking into all form submit events and modifying an old UB data structure - why not update to using:

    //Handles doing something before submit the UB form:
      window.ub.hooks.beforeFormSubmit.push(function() {
       // Update the UB form redirect url"
          window.ub.form.url = "urlGoesHere.com";
      });

Now in terms of the actual code logic this could be simplified by the use of a switch case statement. From your description the following scenarios are such:

  1. < 500 -750 K = https://www.btacademy.com/download/thankyou/

  2. Everyone else goes to https://www.btacademy.com/download/chatwithus/

I get the feeling there is actually a third condition based upon the template selection, but I’m not entirely sure what you need from it as it does the same as everything else currently.

With the current logic in mind you could write something along the lines of the following (aircode written within an IIF, plain javascript):

    (function() {

    window.ub.hooks.beforeFormSubmit.push(function(){
            var option = document.querySelector('select#your_2019_gross_revenue').val;
            var formUrl = window.ub.form.url;
            switch(option.includes("million")){

                //As the option includes "million" it returns true and falls in the larger bracket:
                case true:

                //Within this statement, if they had checked the lets talk box then the url would be chatwithus, else it would default to thank you:      
     if(document.querySelector('input#my_preferred_follow_up_is_send_me_the_templates_and_lets_talk_about_how_you_can_help_my_business').checked){
                  formUrl = "https://www.btacademy.com/download/chatwithus/";
                } else {
                formUrl = "https://www.btacademy.com/download/thankyou/";
                }
                break;

                //As the option does not include "million" it returns false and thus falls in the smallest bracket:
                case false:
                formUrl = "https://www.btacademy.com/download/thankyou/";
                break;

                //In case anything went wrong the url would default to thank you:
                default:
                formUrl = "https://www.btacademy.com/download/thankyou/";

            }
        })

    })();

The if else statement could of course be omitted entirely if there are only two scenario cases, but I wanted to give you an idea of how you might work it in.

As always this is just theoretical aircode and should be tested, adapted and understood before being deployed on any published page iteration. That being said I hope it gets you on the right track to creating a cleaner method for handling your problem.

Userlevel 3
Badge

Hey @asifwebdev that is awesome, thank you so much for sharing! 💪

<script>
$("input[name='purchaseoptions']").change(function() {
var checkedEl = $("input[name='purchaseoptions']:checked");

if (checkedEl.val() === 'One-time purchase: $20') {
window.ub.form.url = "https://domain.com/landingpage1";
} else {
window.ub.form.url = "https://domain.com/landingpage2";
}
});
</script>

This worked for me for radio buttons

Userlevel 5

Eliot,

Here is a script that an Unbounce team member had put together. You should be able to edit your field names and get along just fine if you are comfortable with a little javascript editing.

Badge

I got this same script from Unbounce tech support, but it doesn’t seem to be working.

I’m testing on this page: https://my.caredetective.com/landing-page-test-area/

I’ve set the form confirmation to “Go to URL” and have it pointed to: https://my.caredetective.com/upsell-test/

And the JS code I added looks like this (I’m no coder, just to be clear):

image

However, at this point “Self” is not going anywhere different. Any thoughts? :-/

Badge

Can somebody please help me. I am using a script that I have used perfectly in the past but for some reason, I can not get it to work on this page. I am trying to send users to one url if the select “Yes” in the drop down and to another if the select “No” in the dropdown. Here is the scrip I am using: (I have it set to Before End Body Tag"


$("#business_bank_account").live(‘change’, function() {

switch ($(this).val()) {

case 'Yes':
    window.module.lp.form.data.confirmData = "https://approvals.xxxxxxx.com/business-funding-thank-you/";
    break;
case 'No':
    window.module.lp.form.data.confirmData = "https://approvals.xxxxxxx.com/business-funding-thank-you2/";
    break;

}

});

</script>


For some reason the page is ignoring the script. The field name is correct and I have it set to “go to URL” and append is checked. There is a fallback url set as well and that is always where it is taking the user. Please help

Userlevel 7
Badge +3

Hey @Brett_Rosenblatt,

You need to adjust your script. Take a look one post above you for an example of what you need to change (confirmData to url).

Best,
Hristian

I am using the adjusted script but it still isn’t working. Below is my script:

$("#optin1").live(‘change’, function() {

switch ($(this).val()) {

case 'Yes':
    window.module.lp.form.data.url = "https://URL1.com";
    break;
case 'No':
    window.module.lp.form.data.url = "https://URL2.com";
    break;

}

});

It just keeps sending me to the deafult URL set in the “Properties” section of the builder.

Userlevel 5

Hey James, could you share the link to the page this is running on?

It would be helpful to take a look. Thanks!

http://woodgroupmortgage.com/buy-a-home-waco/

Userlevel 5

Looks like your script has a typo and the console says it’s not a function.

Try pasting this into your Unbounce page…

<script>
$("#optin1").live('change', function() {
switch ($(this).val()) {
case 'Yes':
window.module.lp.form.data.url = "https://fwytrk.com/d.ashx?ckm_campaign_id=188&ckm_key=LG9TsxmdK98&ckm_bp=1";
break;
case 'No':
window.module.lp.form.data.url = "https://fwytrk.com/d.ashx?ckm_campaign_id=136&ckm_key=LG9TsxmdK98&ckm_bp=1";
break;
};
});

I hope that works. Otherwise, I dunno 🤷

It’s not working… i follow the instruction too…

Hi everybody,

As others I am trying to redirect the user based on on the option they used in a drop down menu.

However, it’s not working, it always redirects to the fallback url.

I’ve used the following options

Custom drop down menu

Name & Id is “test”

first menu choice is “taz”

second menu choicen is “zeit”

Form Confirmation is “Go to URL”

URL is “https://www.spiegel.de

Target “Same tab or window”

Append form data to url is unchecked

This is the code I have in the javascipt window:

https://paste.ofcode.org/33sbrUU4Ekb22nqt8kqJUNS

Badge

HI Johannes,

Here’s how I’m doing it on my page. Your code looks the same as mine, EXCEPT you have a semi-colon after the curly brace in the 2nd to last line… I’m no code expert, but could that be causing a problem?

My drop-down selections are as follows:

Who are you providing care for? (field is p_relation)

  • myself

  • spouse

  • parent

  • other

Depending on what they choose i send them to 2 different pages. JS below…

–Kirsti

Here’s my JS: https://paste.ofcode.org/bKkgmpZD4PS3g8eVtHfegJ

Hi, thanks for your reply. Sadly, removing the semi-colon didn’t change anything.

Checking in!
Has anyone figured this out?
I’ve followed this thread through and through and it’s still not working.

Can you do this on a text field using a number range?

Example:
<20 goes to url1, >20 goes to url2

Reply