functions - Contact Form 7 If Condition

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

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 );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744839452a4596466.html

相关推荐

  • functions - Contact Form 7 If Condition

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

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信