jQuery(document).ready(function($){ // Hide initial states $('.apcf-ajax-loader').hide(); $('.ap-error-msg').hide(); $('.ap-success-message').hide(); $('.apcf-captcha-error').hide(); // For captcha Show/hide in frontend var captcha_show = $('.ap-contact-field-wrap-captcha').attr('data-tab'); // Frontend Form Submit Function $('.ap-contact-form-wrap form').submit(function(){ // Specifing $(this) state to a variable to access fields on only this instance var selector = $(this); var error_flag = 0; // Checking for each required field selector.find('.ap-required-field').each(function(){ var value = $(this).val(); if(value == ''){ error_flag = 1; $(this).closest('.ap-contact-field-wrap').find('.ap-error-msg').show(); } }); // Checking state of error flack for submission. if(error_flag == 1){ return false; } else{ selector.find('.apcf-ajax-loader').show(); // Getting value from frontend form to set to ajax data var name = selector.find('.ap-name-field').val(); var email = selector.find('.ap-email-field').val(); var subject = selector.find('.ap-subject-field').val(); var message = selector.find('.ap-message-field').val(); var captcha_val = selector.find('#g-recaptcha-response').val(); // Checking value for captcha show if(captcha_show == 'show'){ if (captcha_val != ''){ $('.apcf-captcha-error').hide(); // Ajax function $.ajax({ url : apcf_js_obj.ajax_url, type : 'post', data:{ action: 'apcf_sendmail', _wpnonce: apcf_js_obj._wpnonce, name:name, email:email, subject: subject, message: message, captchaResponse: captcha_val }, success: function(response){ //alert(response); if(response == 'success'){ selector.find('.apcf-ajax-loader').hide(); if (selector.find('#g-recaptcha-response').length > 0) { grecaptcha.reset(); } selector.find('.ap-error-msg').hide(); selector.find('.ap-success-message').show(); selector.find('.ap-name-field').val(""); selector.find('.ap-email-field').val(""); selector.find('.ap-subject-field').val(""); selector.find('.ap-message-field').val(""); } else{ //alert('Failure'); } console.log("success"); } }); } else{ $('.apcf-captcha-error').show(); } } else{ // Form Submission Ajax call when Captcha is disabled. // alert('captcha deactivated'); $.ajax({ url : apcf_js_obj.ajax_url, type : 'post', data:{ action: 'apcf_sendmail', _wpnonce: apcf_js_obj._wpnonce, name:name, email:email, subject: subject, message: message, }, success: function(response){ //alert(response); if(response == 'success'){ selector.find('.apcf-ajax-loader').hide(); selector.find('.ap-error-msg').hide(); selector.find('.ap-success-message').show(); selector.find('.ap-name-field').val(""); selector.find('.ap-email-field').val(""); selector.find('.ap-subject-field').val(""); selector.find('.ap-message-field').val(""); } else{ //alert('Failure'); } console.log("success"); } }); } } // return true; return false; }); $('.ap-required-field').keyup(function(){ $(this).parents('.ap-contact-field-wrap').find('.ap-error-msg').hide(); }); });