jquery - how to submit form and stop submit form based on ajax response -
i using form onsubmit tag. , in onsubmit function call ajax , return true in success function , false in error section. form has been submitted.
html
<form action="/" method="post" onsubmit="return formsubmit()">
script
function formsubmit() { "use strict"; var isbasic = $(".test").val() //this hidden element if (isbasic === "true") { return true; } if (isbasic === "false") { $.ajax({ type: "post", url: "/", data: "sample", success: function (data) { if (data.success) { return true; } if (!data.success) { return false; } }, error: function () { } }); } }
}
this doesnt work me. form submitted. please me resolve this.
you correct in desiring return value "formsubmit" function, can't return async call (the ajax call async).
restructure code capture button-click instead of responding form submit. in button click event execute ajax call, passing callback function ajax call. in callback function decide whether submit or not submit form.
Comments
Post a Comment