Darwin Tech
Darwin Tech

Reputation: 18919

jQuery - can't call ajax on form submit in Firefox

I have a form which, upon submission, also sends some other data to the server with ajax:

$('#continue-button').on("click", function(e){
    updateSessionVariables();
});

function updateSessionVariables(){
    $.ajax({
        type: "GET",
        url: update_bom_vars,
        data: myVars
    });
});

This works fine in Chrome but does not submit the ajax call in Firefox.

I have tried:

$('#continue-button').on("click", function(e){
    e.preventDefault();
    updateSessionVariables();
});

Now, this sends the data correctly, but the form will not submit. I even added:

     $('#continue-button').submit();

But no joy.

Upvotes: 2

Views: 1923

Answers (4)

Darwin Tech
Darwin Tech

Reputation: 18919

So I actually solved the problem like this:

$('#continue-button').on("click", function(e){
    e.preventDefault();
    updateSessionVariables();
});

function updateSessionVariables(){
    $.ajax({
       type: "GET",
       url: update_bom_vars,
       data: myVars
       success: ajaxComplete
    });
});

function ajaxComplete(){
    $('#parts-form').submit();
}

It seems that you must be sure the ajax call is complete before the submit can work.

Thanks for all the input!

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

You have to submit the form, not the button:

$('#formwithid').submit();

Upvotes: 0

MG_Bautista
MG_Bautista

Reputation: 2653

In te line...

$('#continue-button').submit();

Change for:

$('#myFormId').submit();

Greetings...

Upvotes: 0

PlantTheIdea
PlantTheIdea

Reputation: 16359

Unless continue-button is the id of your form, it wont submit. I'd stick with regular JavaScript here, its a little lighter and all you need:

document.getElementById('formId').submit();

add that inside your click function and you should be good.

Edit: just reread and realized you're not having a successful AJAX call on firefox, and that was the real question. My bad.

If you are sending information to the database, try using POST instead of GET. GET is meant to do just that ... GET information, not submit it.

Upvotes: 0

Related Questions