admin管理员组

文章数量:1404589

I have used the following code to successfully redirect a form to an URL after submission using contact form 7.

<add_action( 'wp_footer', 'redirect_cf7' );

function redirect_cf7() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
   if ( '947' == event.detail.contactFormId ) { // Sends sumissions on form 947 to the first thank you page
    location = '/';
    } else if ( '1070' == event.detail.contactFormId ) { // Sends submissions on form 1070 to the second thank you page
        location = '/';
    } else { // Sends submissions on all unaccounted for forms to the third thank you page
        location = '/';
    }
}, false );
</script>
<?php
} 

Can this be modified to redirect once a certain field contains a certain value (for example [Post-Code] Contains KA7) and another field contains a value (for example [Bedrooms] == 1) ?

Thanks in advance

Andy

I have used the following code to successfully redirect a form to an URL after submission using contact form 7.

<add_action( 'wp_footer', 'redirect_cf7' );

function redirect_cf7() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
   if ( '947' == event.detail.contactFormId ) { // Sends sumissions on form 947 to the first thank you page
    location = 'https://www.example/thank-you-1/';
    } else if ( '1070' == event.detail.contactFormId ) { // Sends submissions on form 1070 to the second thank you page
        location = 'https://www.example/thank-you-2/';
    } else { // Sends submissions on all unaccounted for forms to the third thank you page
        location = 'https://www.example/thank-you-3/';
    }
}, false );
</script>
<?php
} 

Can this be modified to redirect once a certain field contains a certain value (for example [Post-Code] Contains KA7) and another field contains a value (for example [Bedrooms] == 1) ?

Thanks in advance

Andy

Share Improve this question edited Mar 19, 2019 at 12:22 Andy Strachan asked Mar 19, 2019 at 11:46 Andy StrachanAndy Strachan 11 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, you should be able access the form fields and field values with event.detail.inputs in addEventListener on wpcf7mailsent. You can then use the field values in the conditional statements and add the redirects you need. There's a simple code sample in the plugin doumentation, https://contactform7/dom-events/, for looping the fields.

EDIT Here's a code example,

document.addEventListener( 'wpcf7mailsent', function( event ) {

  var inputs = event.detail.inputs,
      inputCount = inputs.length,
      firstCondition,
      secondCondition;

  for ( var i = 0; i < inputCount; i++ ) {
    if ( 'first-condition-field' == inputs[i].name ) {
      firstCondition = inputs[i].value
    } else if ( 'second-condition-field' == inputs[i].name ) {
      secondCondition = inputs[i].value
    }
  }

  if ( 'foo' == firstCondition && 'bar' == secondCondition ) {
    location = 'https://www.example/thank-you-1/';
  } else if ( 'bar' == firstCondition && 'baz' == secondCondition ) {
    location = 'https://www.example/thank-you-2/';
  }

}, false );

本文标签: functionsContact Form 7 If Condition