Skip to content

Instantly share code, notes, and snippets.

@mwunsch
Created December 4, 2009 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwunsch/249149 to your computer and use it in GitHub Desktop.
Save mwunsch/249149 to your computer and use it in GitHub Desktop.
var Messages = {
loader: jQuery('<div class="loading">Loading...</div>'),
thanks: jQuery('<div class="success">Thanks! We\'ll stay in touch!</div>'),
error: jQuery('<div class="ajax-error">Oh no. Something really terrible happened</div>'),
};
var contact_form = jQuery('#contact-form');
var colorBox = jQuery.fn.colorbox;
jQuery('#email-form').validate({
submitHandler: function(form) {
var $this = jQuery(form);
$this.hide(); //this hides the form
if (contact_form.find('.loading').length <= 0) {
contact_form.prepend(Messages.loader);
} else {
contact_form.find('.loading').show();
}
colorBox.resize();
jQuery.ajax({
type: "POST",
url: $this.attr("action"),
// this submits the form to the same place as described
// in the form action attribute
data: $this.serialize(),
success: function(data, status) {
contact_form.find('.loading').hide();
if (contact_form.find('.success').length <= 0) {
contact_form.append(Messages.thanks);
} else {
contact_form.find('.success').show();
}
colorBox.resize();
},
error: function(xhr, status, error) {
contact_form.find('.loading').hide();
if (contact_form.find('.ajax-error').length <= 0) {
contact_form.prepend(Messages.error);
} else {
contact_form.find('.ajax-error').show();
}
// error message, probably bring the form back
$this.show();
colorBox.resize();
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment