I have form where before i was using simple post with <button name="submit" id="register" value="Register">Continue</button>
than i decided to change that post to jquery ajax post:
<script type="text/javascript">
$(document).ready(function () {
$("#register").click(function () {
if (!$("form").valid())
return false;
var isContinue = false;
$.ajax({
url: "/Validate",
type: "POST",
dataType: "text",
data: { recaptcha_challenge_field: $("input#recaptcha_challenge_field").val(), recaptcha_response_field: $("input#recaptcha_response_field").val() },
cache: false,
success: function (data, textStatus, xhr) {
isContinue = data === 'true';
},
error: function (xhr, st, err) {
alert("Sorry error occured, try again later.");
},
complete: function () {
alert(isContinue);//i could see that alert
if (isContinue) {
alert(isContinue); //i could see that alert
$("form").submit();
alert(isContinue); //i could see that alert
}
else {
return false;
}
}
});
});
});
</script>
<a class="button" id="register">Continue</a
So if all validation including capthca passed than when i clicking to submit button and form does not posted on server. I checked request with chrome and opera network dev tools and they didn’t show that post action, but according with alerts which i placed in javascript the form should be posted.
Then for test i placed another simple submit button and form getting posted so for some reason $("form").submit(); don’t want to post the form.
May be some one may have any ideas what it could be?
UPDATE
I was commenting the ajax post button when i tried simple post button but the thing is
i was having another back button 🙂 which is looking like that:
<button name="submit" class="cancel" id="back" value="Back">
so the problem is really with name="submit", i just remove that and all working now.
It would take very long time to figure out that the problem just with name="submit", thanks for help.
This is because the button you’ve named
submitis overriding thesubmitmethod on theformelement, so instead ofform.submitbeing a function that submits the form, it’s now a reference to thebuttonelement.The easiest way of fixing this is to rename your
buttonelement.It’s important to differentiate between
$('form').submit()andform.submit().$('form').submit)is a jQuery method that is not affected by the button named submit, however behind the scenes, jQuery calls thesubmitattribute on the actualHTMLFormelement, which is affected by the button namedsubmit.There is a bug about this on http://bugs.jquery.com, however as described above, it’s not actually a problem with jQuery itself.
You’re code has another problem; you shouldn’t be using
$('form')inside your click handler, as this is building a jQuery object to alllll forms on your page; so you’re checking that alllll forms are valid, and submitting alll forms. Instead, use$(this).closest('form')