Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created February 6, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihorduchenko/49fa0649da4df6bab72c71a3ba2aa07f to your computer and use it in GitHub Desktop.
Save ihorduchenko/49fa0649da4df6bab72c71a3ba2aa07f to your computer and use it in GitHub Desktop.
Prevent equal multiple Contact Form 7 submissions using cookies
var Cookie = {
Set: function (name, value) {
var expires = "";
var date = new Date();
date.setTime(date.getTime() + (5 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + value + expires + "; path=/";
},
Get: function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
};
document.addEventListener("DOMContentLoaded", function(event) {
// Getting cookies
var cookie_name = Cookie.Get("form_name");
var cookie_email = Cookie.Get("form_email");
var cookie_phone = Cookie.Get("form_phone");
// Checking if exist
var isNameSet = cookie_name != null;
var isEmailSet = cookie_email != null;
var isPhoneSet = cookie_phone != null;
// Our form DOM elements
var wpcf7cp = document.querySelector( '.contact-page-form' );
var wpcf7Elm = document.querySelector( '.wpcf7' );
function cookieForm(el) {
el.addEventListener( 'wpcf7submit', function( event ) {
var input_name = el.querySelector( 'input.cookie-name' );
var input_email = el.querySelector( 'input.cookie-email' );
var input_phone = el.querySelector( 'input.cookie-phone' );
// Form inputs to be removed
formBlocks = el.querySelectorAll('p');
formColumns = el.querySelectorAll('.column');
var name = input_name.value;
var email = input_email.value;
var phone = input_phone.value;
// Checking cookies and compare with inputs
if(!isNameSet) {
Cookie.Set('form_name', name)
} else {
if(cookie_name == name) {
event.preventDefault();
}
}
if(!isEmailSet) {
Cookie.Set('form_email', email)
} else {
if(cookie_name == name) {
event.preventDefault();
}
}
if(!isPhoneSet) {
Cookie.Set('form_phone', phone)
} else {
if(cookie_name == name) {
event.preventDefault();
}
}
// Hiding content elements if submission is first
if(!isNameSet && !isPhoneSet && !isEmailSet ) {
[].forEach.call(formBlocks, function(div) {
div.style.display = "none";
div.parentNode.removeChild(div);
});
[].forEach.call(formColumns, function(div) {
div.style.display = "none";
div.parentNode.removeChild(div);
});
}
}, true );
var sbmt = el.querySelector( 'input.wpcf7-submit' );
var resp = el.querySelector( '.wpcf7-response-output' );
var spamMessage = document.getElementById('spam-message').innerHTML;
sbmt.addEventListener( 'click', function( event ) {
var input_name = el.querySelector( 'input.cookie-name' );
var input_email = el.querySelector( 'input.cookie-email' );
var input_phone = el.querySelector( 'input.cookie-phone' );
var name = input_name.value;
var email = input_email.value;
var phone = input_phone.value;
// Showing alert message below the form
if(name == cookie_name) {
event.preventDefault();
resp.innerHTML = spamMessage;
resp.style.display = 'block';
}
if(email == cookie_email) {
event.preventDefault();
resp.innerHTML = spamMessage;
resp.style.display = 'block';
}
if(phone == cookie_phone) {
event.preventDefault();
resp.innerHTML = spamMessage;
resp.style.display = 'block';
}
}, true );
}
// Initialization
if(wpcf7cp) {
cookieForm(wpcf7cp);
}
if(wpcf7Elm) {
cookieForm(wpcf7Elm);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment