在使用联系人表单7提交表单后,我使用以下代码成功地将表单重定向到URL。
<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.com/thank-you-1/\';
} else if ( \'1070\' == event.detail.contactFormId ) { // Sends submissions on form 1070 to the second thank you page
location = \'https://www.example.com/thank-you-2/\';
} else { // Sends submissions on all unaccounted for forms to the third thank you page
location = \'https://www.example.com/thank-you-3/\';
}
}, false );
</script>
<?php
}
当某个字段包含某个值(例如[邮政编码]包含KA7)而另一个字段包含某个值(例如[卧室]==1)时,是否可以修改此选项以重定向?
提前感谢
安迪
SO网友:Antti Koskinen
是的,您应该能够使用访问表单字段和字段值event.detail.inputs
在里面addEventListener
在…上wpcf7mailsent
. 然后可以使用条件语句中的字段值并添加所需的重定向。插件中有一个简单的代码示例,https://contactform7.com/dom-events/, 用于循环字段。
EDIT下面是一个代码示例,
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.com/thank-you-1/\';
} else if ( \'bar\' == firstCondition && \'baz\' == secondCondition ) {
location = \'https://www.example.com/thank-you-2/\';
}
}, false );