I suggest nixing out this jQuery stuff to get more maintainable code. Depending on what you need to do you may have to expand on this but as you can see is doing the same and costing much less.
Hi
I have a question about this similar topic above. Maybe someone can help me.
I have one form where I use the Ajax form action. If I push the submit button, the Ajax call happens in the background and in foreground it shows the Success- or Errormessage. This works perfect.
But if I have two forms or more, it doesnāt work anymore. How can I spezify the form for the Ajax call? Should I give a ID or a name? What should i change/add in code below?
<script>
makeWebflowFormAjax = function( forms, successCallback, errorCallback ) {
forms.each(function(){
var form = $(this);
form.on("submit", function(){
var container = form.parent();
var doneBlock = $(".w-form-done", container);
var failBlock = $(".w-form-fail", container);
var action = form.attr("action");
var method = form.attr("method");
var data = form.serialize();
// call via ajax
$.ajax({
type: method,
url: action,
data: data,
success: function (resultData) {
if (typeof successCallback === 'function') {
// call custom callback
result = successCallback(resultData);
if ( ! result ) {
// show error (fail) block
form.show();
doneBlock.hide();
failBlock.show();
console.log(e);
return;
}
}
// show success (done) block
form.hide();
doneBlock.show();
failBlock.hide();
},
error: function (e) {
// call custom callback
if (typeof errorCallback === 'function') {
errorCallback(e)
}
// show error (fail) block
form.show();
doneBlock.hide();
failBlock.show();
console.log(e);
}
});
// prevent default webdlow action
return false;
});
});
}
var Webflow = Webflow || [];
Webflow.push(function() {
// === Custom Form Handling ===
// unbind webflow form handling
$(document).off('submit');
makeWebflowFormAjax( $("form") );
// new form handling
$('form').submit(function(evt) {
evt.preventDefault();
});
});
</script>
@TWith - do you have a published page I can look at? The function should work for multiple forms, based on the .each() function. However, Iām not sure Iāve ever tried on a page with multiple forms so I canāt say for sure without looking.
Sorry about total noobie questions, but Iām unsure about implementing this.
So the main long code goes (in settings) inside the head tag or before body?
What is the URL that goes in the form settings action field?
Or is it an external form code address that goes into the action field? If so what code is that?
Hi, maybe a little to late, but I have come with this post today that I was struggling with this same problem. I use the exact piece of code that you post it, but if you want to link it to a specific form you have to name it where it says $(āformā) and $(āformā), replace āformā with (in my case use the id of the form) $(ā#id_of_your_formā). That should work perfectly.