How can I use the jQuery $.ajax function to stop or allow a form submit?

ajax, jquery

Solution

You could do it like this:

$.ajax({
    url: 'add.html',
    data: $('#post_form').serialize(),
    dataType: 'json',
    type: 'post',
    success: function( result ) {
        retVal = formCheck( result );
        if(retVal)
          $('#post_form').unbind('submit').submit();
        else
          alert('Error with your form! Oh crap!');
    }
});

Since you're posting back and leaving, this just unbinds this handler if successful and lets the submit happen normally when invoking it, so the code doesn't run and check again, it just submits.

Problem

``` $('#post_form').submit(function() { $("#ajax_bar_loader").show(); $.ajax({ url: 'add.html', data: $('#post_form').serialize(), dataType: 'json', type: 'post', success: function( result ) { retVal = formCheck( result ); } }); return false; }); ``` That's what my code looks like and instead of the "return false", I want to allow it if the data contained in "result" says to. How can I accomplish this?

Original source