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 badges1 Answer
Reset to default 1Yes, 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
评论列表(0条)