I have the following code in my theme file, but it doesnt seem to be working on submission of the form, I cant seem to work out why. Can anyone see the issue? :
add_action('wp_footer', 'redirect_page');
function redirect_page()
{
?>
<script type="text/javascript">
document.addEventListener('wpcf7mailsent', function(event) {
console.log(event.detail);
if ('95' == event.detail.contactFormId) {
console.log(event.detail.inputs['menu314']);
var selectedValue = event.detail.inputs['menu314'];
if (selectedValue == 'Download Guide for DFG teams') {
window.location.href = 'url.pdf';
} else if (selectedValue == 'Download Guide for Non-specialists') {
window.location.href = 'url.pdf';
} else if (selectedValue == 'Download Guide for Managers') {
window.location.href = 'https://url.pdf';
}
}
}, false);
</script>
<?php
}
CF7 Form:
[contact-form-7 id="95" title="Subscribe download"]
<label class="uabb-cf7-col-1">[text* your-name placeholder "Name"]</label>
<label class="uabb-cf7-col-1">[tel tel-985 placeholder "Telephone"]</label>
<label class="uabb-cf7-col-1"> [email* your-email placeholder "Email"] </label>
[select* menu-312 id:menu314 "Select Guide" "Download Guide for DFG teams" "Download Guide for Non-specialists" "Download Guide for Managers"]
<label class="uabb-cf7-col-1">[submit "Download Guide"]</label>
console is showing no errors, form sends, but PDF is not redirected
2
Answers
I think you should use CF7 DOM events
enter link description here
for example:
To get the value of input from the
event.detail
inputs is an object with the data in an array containingname
andvalue
. The array keys have nothing to do with the names or id’s of the fields, just an array.Also, the contactFormId is an integer, for whatever that’s worth.
So if you did.
where inputs[3] is the fourth field (which is your select), then you should get where you want to go. If you
console.log(event.detail.inputs);
and expand the array to see the data, you should see the key if it’s not[3]