hypothetical/resources/assets/js/contact.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

// contact form functionality
2016-01-03 19:08:53 -05:00
function contactFormInit() {
var $form = $('#contact-form'),
$input = $form.find(':input'),
2016-01-03 19:08:53 -05:00
$notify = $('#notification'),
contact = {},
submitting = false;
var getContactData = function() {
contact = {
name: $('#name').val(),
email: $('#email').val(),
message: $('#message').val(),
_token: $('#token').val()
};
};
$('#submit').on('click', function(e) {
e.preventDefault();
var $submit = $(this);
2016-01-03 19:08:53 -05:00
if (!submitting) {
submitting = true;
getContactData();
$.ajax({
type: 'POST',
url: '/contact-submit',
data: contact
}).always(function(response) {
$form.find('.error').removeClass('error');
$notify.removeClass('visible');
if (response === 'success') {
$input.attr('disabled', true);
$submit.addClass('disabled');
2016-01-03 19:08:53 -05:00
$notify.text('Thanks for your message!').addClass('success').addClass('visible');
} else {
var responseJSON = response.responseJSON,
errors = 0;
// add the error class to fields that haven't been filled out
for (var prop in responseJSON) {
if (responseJSON.hasOwnProperty(prop)) {
$('#' + prop).addClass('error');
errors++;
}
}
if (errors > 0) {
$notify.find('span').text(errors);
$notify.addClass('visible');
}
// re-enable submitting
submitting = false;
}
});
}
});
}