I have a form gateway for PDF's. The user submits a form, then recieves a PDF by email and redirects them to the PDF url. The problem I have is the redirect seems to work when the required form fields are left empty. I briefly see the field error messages, before it redirects the user to the PDF regardless. Am I missing something?
This is different to Using JQuery - preventing form from submitting. If the required fields haven't been filled in, the jQuery .submit() and the contents within shouldn't do anything. I've added .preventDefault()
but it still fires when the submit button is clicked without the required fields filled.
The issue is the jQuery(location).attr('href', productDownloadFile)
line. This shouldn't happen without correct fields filled.
My code:
jQuery('#gform_5').submit(function (e) {
e.preventDefault();
var pathname = window.location.pathname;
jQuery.cookie('cookie-name', pathname, {path: pathname}); // Set cookie to prevent multiple form entries.
// Close modal on form submit and redirect to the PDF.
setTimeout(function() {
jQuery('.pdf-gateway').removeClass('is-visible');
jQuery(location).attr('href', productDownloadFile); // productDownloadFile set in functions.
}, 3000);
});