Created
March 3, 2019 18:15
-
-
Save davidwerks/70746026c078792ace9a80665e988158 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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