Skip to content

Instantly share code, notes, and snippets.

@davidwerks
Created March 3, 2019 18:15
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 davidwerks/70746026c078792ace9a80665e988158 to your computer and use it in GitHub Desktop.
Save davidwerks/70746026c078792ace9a80665e988158 to your computer and use it in GitHub Desktop.
//apply only to forms with the action pointing to Basin
$('form[action^="https://usebasin.com"]').each(function(i,el){
console.log($(el));
$(el).submit(function(e){
//stop the form from submitting
e.preventDefault();
const _form = $(e.target);
// form parent
const _parent = $(_form.parent());
//get the form's action parameter and add ".json" to the end
const _action = _form.attr('action') + '.json';
console.log(_action);
//submit the form via ajax
$.ajax({
url: _action,
method: "POST",
data: _form.serialize(),
dataType: "json",
success: function(data){
if(data.success){
//successful submission - hide the form and show the success message
_parent.children('form').css('display','none');
_parent.children('.w-form-done').css('display','block');
} else {
//failed submission - log the output to the console and show the failure message
console.log(data);
_parent.find('.w-form-fail').css('display','block');
}
},
error: function(){
//failed submission - show the failure message
_parent.find('.w-form-fail').css('display','block');
console.log(_parent);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment