Reputation: 9049
my question is probably really simple, but i'll ask anyways.
So I have a submit button in my form that has an onclick="ajax()" and at the same time the action of the form is action=""
I notice at the execution of my form the page it is sill redirecting to whatever is in my action code.
What should I be doing when doing ajax form submissions?
Upvotes: 0
Views: 155
Reputation: 11095
If you're using jQuery do it jQuery's way (do not call the function in the onclick
):
$(document).ready(function () {
$("#buttonId").click(function (event) {
event.preventDefault();
// do submit here
});
});
Or use malsup's AJAX form plugin.
Upvotes: 2
Reputation: 57968
what you need to do is preventDefault
form you ajax function so that the submit doesnt get triggered
so it would look like this:
onclick="ajax(event)"
function ajax(e){
...
e.preventDefault();
}
Upvotes: 1
Reputation: 114417
Submit buttons submit your form. Use a regular button. input type="button"
Upvotes: 4