Skip to content

Instantly share code, notes, and snippets.

@ioelada
Forked from rustyeddy/contact-form-7-redirect.js
Created January 25, 2018 00:36
Show Gist options
  • Save ioelada/ff54a9af962eff5814fd8e91a898d35a to your computer and use it in GitHub Desktop.
Save ioelada/ff54a9af962eff5814fd8e91a898d35a to your computer and use it in GitHub Desktop.
Contact Form 7 - Redirect to a different thank you page based on input from from a pull down select form field.
/**
* Contact form 7 - Redirect to a specific page based on the value of a form
* field. In this specific example I created a drop down menu item and gave
* it an id of 'select-menu', I also created .
*/
function cf7_redirect() {
// Set the base url for contact-form-7
var url = document.referrer;
// I used the HTML ID 'select-menu' for this example
var page = document.getElementById('select-menu').value;
/**
* The options for the select are "Page 1", "Page 2" & "Page 3".
* The urls are the permalinks not including the base url 'page-1', etc.
*/
switch (page) {
case "Page 1":
url = url + 'page-1';
break;
case "Page 2":
url = url + 'page-2';
break;
case "Page 3":
url = url + 'page-3';
break;
default:
url = null;
}
// Now redirect if everything went well
if ( url !== null ) {
location.replace(url);
} else {
alert("Error with selection: " + page);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment