Skip to content

Instantly share code, notes, and snippets.

@0test
Created February 27, 2023 12:32
Show Gist options
  • Save 0test/8b0dd09b28fce2fae9b6967a3b007518 to your computer and use it in GitHub Desktop.
Save 0test/8b0dd09b28fce2fae9b6967a3b007518 to your computer and use it in GitHub Desktop.
jQuery(document).ready(function(){
jQuery('#contacts_form').on('submit',function(){
form_id = $(this);
jQuery.ajax({
type: 'POST',
url: '/ajax/contacts',
data: new FormData($(this)[0]),
dataType: 'json',
cache: false,
processData: false,
contentType: false,
success: function(data) {
$(form_id).find('.invalid-feedback').html('');
$(form_id).find('input,textarea').removeClass('is-invalid')
if(data.status === true){
$(form_id).html(data.output)
}
else{
$.each(data.errors, function(index,item) {
var error_text = '';
var validate_errors_types = Object.keys(item);
for(var key in validate_errors_types){
var error_text = item[validate_errors_types[key]];
var field = $(form_id).find('[name="' + index + '"]');
field.addClass('is-invalid');
field.siblings('.invalid-feedback').html(error_text);
}
});
}
}
});
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment