jQuery(document).ready(function($) {
$('#custom-form').submit(function(e) {
e.preventDefault(); // Prevent the default form submission
var formData = $(this).serialize(); // Serialize the form data
var marketingConsent = $('#marketing-consent').is(':checked') ? 'true' : 'false'; // Check the checkbox state
formData += '&marketing_consent=' + marketingConsent;
// Show the spinner and disable the submit button
$('#spinner').show();
$('#submit-btn').prop('disabled', true).css('cursor', 'not-allowed');
$.ajax({
type: 'POST',
url: myAjax.ajaxurl, // Use the localized script variable
data: {
action: 'authenticate_odoo_user', // Your custom action hook
form_data: formData
},
success: function(response) {
// Hide the spinner and enable the submit button
$('#spinner').hide();
$('#submit-btn').prop('disabled', false).css('cursor', 'pointer');
if(response.success) {
// Show the lead ID in the response block
$('#form-response').html('Thanks for submitting! The file will be downloaded shortly.');
// Set a 5-second timer to hide the response
setTimeout(function() {
$('#form-response').fadeOut(); // Smoothly fade out the response
}, 5000); // 5000 milliseconds = 5 seconds
// Set a 2-second timer before triggering the PDF download
setTimeout(function() {
var link = document.createElement('a');
link.href = response.data.pdf_url; // PDF URL received from the server
link.download = 'Cybersecurity-Catalogue.pdf'; // Name the file for download
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}, 2000);
} else {
$('#form-response').html('Error: ' + response.data);
}
},
error: function(xhr, status, error) {
$('#spinner').hide();
$('#submit-btn').prop('disabled', false).css('cursor', 'pointer');
console.error('AJAX Error:', status, error);
$('#form-response').html('An error occurred. Please try again.');
}
});
});
});