Adam
Adam

Reputation: 9049

submitting form to ajax function, but it's reloading the page as well

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

Answers (4)

Albireo
Albireo

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

mkoryak
mkoryak

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

Adeel
Adeel

Reputation: 19228

use return false;

onclick="ajax();return false;"

Upvotes: 5

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

Submit buttons submit your form. Use a regular button. input type="button"

Upvotes: 4

Related Questions