• Resolved chiproyce

    (@chiproyce)


    The developer published this great post on how to do redirects from Contact Form 7.

    https://contactform7.com/redirecting-to-another-url-after-submissions/

    The only issue is that I have a number of different forms on my site and the script will trigger the same action for all forms.

    I’ve tried (unsuccessfully) to combine the sample script on the support page with an IF statement that was found on another example:

    <script>
    document.addEventListener( 'wpcf7mailsent', function( event ) {
        if ( 'Cool Quote Form' == event.detail.contactFormId ) {
        location = 'https://www.fusion3design.com/thank/';
    }, false );
    </script>

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    Can someone please suggest a way to designate a way to make the script behave differently depending on which form is used?

    Thank you!

    • This topic was modified 8 years, 6 months ago by chiproyce.
    • This topic was modified 8 years, 6 months ago by chiproyce.
    • This topic was modified 8 years, 6 months ago by bdbrown.
Viewing 8 replies - 1 through 8 (of 8 total)
  • anonymized-15380454

    (@anonymized-15380454)

    event.detail.contactFormId is the contact form ID and not its name.

    See https://contactform7.com/dom-events/.

    So use an ID, like in this example:

    <script>
    document.addEventListener( ‘wpcf7mailsent’, function( event ) {
      // If the form ID is 3, redirects users to https://www.fusion3design.com/thank/
      if ( 3 === event.detail.contactFormId ) {
        location = ‘https://www.fusion3design.com/thank/’;
      }
    }, false );
    </script>

    For several forms (on the same page), you can use the switch statement, like so:

    <script>
    document.addEventListener( ‘wpcf7mailsent’, function( event ) {
      switch ( event.detail.contactFormId ) {
        case 3:
          location = ‘https://www.fusion3design.com/thank/’;
          break;
    
        case 4:
          location = 'URL if form ID is 4';
          break;
    
        case 5:
          location = 'URL if form ID is 5';
          break;
      }
    }, false );
    </script>
    Thread Starter chiproyce

    (@chiproyce)

    Thank you @saltennys!

    The switch statement is exactly what I was looking for, thank you.

    Unfortunately, like others, I am getting the spinning icon issue so the form is not redirecting as intended. Hopefully when that issue is resolved, then we will see how the script is operating.

    anonymized-15380454

    (@anonymized-15380454)

    You’re welcome.

    But I’m not actually getting the spinning icon issue — not with the default code snippet provided and with the latest Contact Form 7 release. So maybe the Contact Form 7 script on your site is in conflict with one or more (JS) scripts?

    You can temporarily disable/deactivate all non- Contact Form 7 plugins and see if the issue persists. If not, it could be your theme.

    anonymized-15380454

    (@anonymized-15380454)

    But anyway, there is a plugin which you might want to try:

    Contact Form 7 Redirection

    I haven’t tried it though.

    Thread Starter chiproyce

    (@chiproyce)

    Thanks @saltennys

    Yes, need to spend the time to diagnose the issue.

    We used to use the Contact Form 7 Redirection plugin, however, my understanding is that it will no longer work due to the phase out of the on_sent_ok command

    anonymized-15380454

    (@anonymized-15380454)

    I see.

    But apart from that plugin and using JavaScript, there is a Contact Form 7 filter you can try:

    wpcf7_mail_sent

    However, it needs PHP coding, like below (which uses closure), and is not to be used/enabled in AJAX submission mode.

    <?php
    add_action( 'wpcf7_mail_sent', function( $contact_form_obj ) {
      // Redirection logic/"switch" goes here
    } );
    ?>

    For an advanced approach, you can copy the Contact Form 7 script, modify it (to add custom redirection logic), and then disable the loading of the original or default JS script.

    Thanks and hopefully you’ll be able to find a workaround for that (spinning icon) issue!

    Thread Starter chiproyce

    (@chiproyce)

    I found the offending plugin and CF7 now working fine (although a bit peeved about losing pop-up capabilities for one of my forms)

    On an aside, the CF7 redirect plugin still works, although I thought that I read that it will become unusable soon due to phase out of a WP command.

    anonymized-15380454

    (@anonymized-15380454)

    You can always try other pop-up plugins. 🙂

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Using Redirect Script – different thank you pages based on form?’ is closed to new replies.