Skip to content
jQuery(document).ready(function($) {
const formSelector = '.elementor-form';
$(document).on('submit', formSelector, function(e) {
let $form = $(this);
let email = $form.find('input[name="form_fields[field_2d672d6]"]').val();
let otpField = $form.find('input[name="form_fields[otp_input]"]');
let otp = otpField.val();
// Case 1: OTP field is empty → send OTP email
if (otpField.is(':hidden')) {
e.preventDefault();
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
dataType: 'json',
data: {
action: 'send_otp', // We'll create this new action below
email: email
},
success: function(response) {
if (response.success) {
alert("✅ Verification code sent to " + email + ". Enter it below to continue.");
otpField.closest('.elementor-field-group').show(); // Show OTP field
} else {
alert("❌ " + response.data.message);
}
},
error: function() {
alert("⚠️ Could not send OTP. Please try again.");
}
});
return false;
}
// Case 2: OTP is entered → verify it before submitting
if (otp) {
e.preventDefault();
$.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'POST',
dataType: 'json',
data: {
action: 'verify_otp',
email: email,
otp: otp
},
success: function(response) {
if (response.success) {
alert("🎉 OTP verified successfully! Submitting form...");
otpField.closest('.elementor-field-group').hide();
$form.off('submit').submit(); // submit for real now
} else {
alert("❌ Invalid or expired OTP. Please try again.");
}
},
error: function() {
alert("⚠️ Something went wrong verifying OTP.");
}
});
return false;
}
});
});