Support » Plugin: Contact Form 7 » Contact Form 7 Conditional redirect based on drop-down menu

  • Resolved shackleman

    (@shackleman)


    I have the following script

    <script type=”text/javascript”>
    document.addEventListener( ‘wpcf7mailsent’, function( event ) {
    var redirector = $(“select[name=number]”).val();
    if(redirector == ‘1’ ){
    location = ‘https://facebook.com&#8217;;
    }
    else if(redirector == ‘5’ ){
    location = ‘https://youtube.com&#8217;;
    }
    else if(redirector == ’10’ ){
    location = ‘https://twitter.com&#8217;;
    } else {
    location = ‘https://myspace.com&#8217;;
    }
    }, false );
    </script>

    I don’t understand why it’s not doing what it’s supposed to. It’s supposed to redirect after submission to a website depending on the selection you make on the drop-down menu, however, it does not.

    So for example, when I select number 10 in the drop-down menu (the dropdown menu ID is “number”) and click submit, it’ll redirect me to twitter.

Viewing 9 replies - 1 through 9 (of 9 total)
  • I came across your post trying to figure out a similar solution and thought I would share. I needed to redirect to a different page based on the select field. Please note, I am not very good with JavaScript, so I will do my best to explain what is going on.

    
    document.addEventListener( 'wpcf7submit', function( event ) {
        if ( '4765' == event.detail.contactFormId ) {
          
          var lpLocation =  document.getElementById("lp-location").value;
    
          if (lpLocation == "Frederick") {
            location = 'http://example.com/';
          } else if (lpLocation == "Annapolis") {
            location = 'http://google.com/';
          }
          
        }
    }, false )  
    
    

    In the second line, 4765 is the form ID. I have multiple forms and only need this to work on one of them.

    On the next line, “lp-location” is the ID for my select field. So my form tag looks like this:

    
    [select* location id:lp-location include_blank "Frederick" "Annapolis"]
    
    

    Hope that helps. Good luck!

    • This reply was modified 1 year, 10 months ago by SethAt8vo.

    It works. Thank you very much!

    @sethat8vo kindly check my code below. It didn’t work. This is to be pasted under additional settings right?
    —————————————————-

    document.addEventListener( ‘wpcf7submit’, function( event ) {
    if ( ‘11914’ == event.detail.contactFormId ) {

    var lpLocation = document.getElementById(“lp-location”).value;

    if (lpLocation == “0 – 2 years”) {
    location = ‘https://houseofshield.com/shop/membership/apprentice-subscription/&#8217;;
    } else if (lpLocation == “3 years and above”) {
    location = ‘https://houseofshield.com/shop/membership/experienced-subscription/&#8217;;
    }

    }
    }, false )

    Hi, @olufemishield. You will need to include the code snippet on the page where the form is located as a script, between an opening and closing script tag. For example, I have mine in the footer.php file of my template.

    Hope that helps.

    I just wanted to add to this thread and the code that @sethat8vo listed.

    When using wpcf7submit, the form will attempt to submit and perform the redirect. However, if a visitor fills out the form but misses a required field, the form will still redirect once the submission button has been clicked even though the form hasn’t been sent to the recipient or actually completed.

    To rectify that and make sure you’re redirected after the form has officially been completed, you can replace wpcf7submit with wpcf7mailsent.

    Hope that helps.

    • This reply was modified 1 year, 6 months ago by bcrab. Reason: Code tags

    Hi,

    Can someone please check this code as it’s currently not working, the form just sends a confirmation email and doesn’t redirect.

    document.addEventListener( ‘wpcf7mailsent’, function( event ) {
    if ( ‘3683’ == event.detail.contactFormId ) {

    var Deposit = document.getElementById(“deposit”).value;

    if (Deposit == “Yes”) {
    location = ‘https://familymattersmediate.co.uk/deposit-payments/’;
    } else if (Deposit == “No”) {
    location = ‘https://familymattersmediate.co.uk/thank-you/’;
    }

    }
    }, false )

    The dropdown menu looks like this

    [select* menu-90 id:deposit include_blank “Yes” “No”]

    the form can be seen here –

    https://familymattersmediate.co.uk/referral-form-test/

    Hello there, many thanks for the contribution, I was able to replicate the example above on my site. Now, I would like to turn the dropdown into a radio button selection with 3 options. So far, I could not make it work. Anyone can help? Thank you.

    This works fine for me.

    However, I’d like to redirect visitors to a page based on their input.

    Say I have 2 variables: their age (3 age groups: young, middle-aged, old) and their interests (3 fields of interest: books, movies, music).

    So, there are a 9 combinations possible.
    If someone is young, and interested in music, I’d want to redirect him to a page about Kanye West
    If somone is young, and interested in books, I’d want to redirect him to a page about Harry Potter.
    If momeone is young and interested in movies, I’d want to redirect him to a page about the movie The Hunger games etc.

    This should be possible with the wpcf7mailsent-command… But how?

    Suggestions are much appreciated.

    It seems this can be done with the Redirection Plugin, an add on for CF7. I have not managed to make it work, but I do have some guidelines. You have to use Javascript. Here’s an example, using one variable (unformatunately not two): https://wordpress.org/support/topic/request-for-multiple-external-redirects/

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Contact Form 7 Conditional redirect based on drop-down menu’ is closed to new replies.