Skip to content

Instantly share code, notes, and snippets.

@paulononaka
Created June 10, 2013 18:29
Show Gist options
  • Save paulononaka/5751066 to your computer and use it in GitHub Desktop.
Save paulononaka/5751066 to your computer and use it in GitHub Desktop.
ajax form
$(document).ready(function() {
$form = $("#signup-form");
$form.bind("ajax:beforeSend", function(evt, xhr, settings){
var $submitButton = $form.find('.btn-submit');
$submitButton.attr("disabled", true);
$submitButton.data('origText', $submitButton.text());
$submitButton.text("Cadastrando..." );
})
.bind("ajax:success", function(evt, data, status, xhr){
$form.find('textarea,.btn-submit,input[type="file"]').val("");
$form.find('.alert-error').hide();
window.location.href = data.url;
})
.bind('ajax:complete', function(evt, xhr, status){
var $submitButton = $form.find('.btn-submit');
$submitButton.attr("disabled", false);
$submitButton.text($submitButton.data('origText'));
})
.bind("ajax:error", function(e, xhr, status, error) {
var errors;
var errorText;
try {
errors = $.parseJSON(xhr.responseText).errors;
} catch(err) {
errors = {message: "Please reload the page and try again"};
}
errorText = "<ul>";
for ( error in errors ) {
errorText += "<li>" + errors[error] + "</li> ";
}
errorText += "</ul>";
$form.find('.alert-error').show();
$form.find('.error-msg').html(errorText);
});
$('#mySignup').on('hidden', function () {
$form.find('.alert-error').hide();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment