tags:

views:

55

answers:

4
$('#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?

A: 
    success: function(result) {
        ...
        document.forms.post_form.submit();
    }
SaltLake
+3  A: 

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.

Nick Craver
In jquery do you need to unbind first? It will submit either way it just wastes rechecking the form correct?
Jeff Beck
@Jeff - Since this happens in the submit handler, this code would run again if you didn't.
Nick Craver
@Nick thanks but it would still submit the even if it is running the return false; correct?
Jeff Beck
@Jeff Beck - the `return false;` will cancel this submit call, the one invoked by a true return on the ajax call won't run the code that contains the return false.
Nick Craver
The problem here is, $.ajax will run asynchronously, so it's quite possible that we won't unbind the submit handler before the form's `action` attribute has been followed.
karim79
@karim79 - It fires off the request though, javascript being single threaded should guarantee this method completes before the success handler is allowed to run.
Nick Craver
Reading the jquery page on the .submit() call it would seem it will re-execute the events bound to the submit but it will still submit regardless of what values they return. I haven't tried this to confirm yet.
Jeff Beck
@Jeff - this call `.unbind('submit')` removes the jQuery bind, so it won't execute again.
Nick Craver
+2  A: 

I would suggest always returning false at the end of the event handler in order to prevent normal form submission. Within the success callback to $.ajax, you could conditionally redirect based on the response from the server:

$('#post_form').submit(function() {
    $("#ajax_bar_loader").show();
    $.ajax({
        url: 'add.html',
        data: $('#post_form').serialize(),
        dataType: 'json',
        type: 'post',
        success: function( result ) {
            if(result == 'something') {
                alert("some error happened");
            } else {
                // assuming we're visiting the form's action
                window.location.href = $(this).attr("action");
            }
        }
    });
    return false;
});

Relying on the success callback to fire before the submit handler completes execution is unrealistic, since the code following the $.ajax call is very likely to be evaluated before the success callback executes ($.ajax will run asynchronously).

karim79
But that wouldn't actually submit any of the form data to the action of the form would it?
Jeff Beck
A: 

I'm not sure if this will work for your problem but what if you made the ajax request synchronous?

I got this dummy code to "work", it fires the ajax request and then sets a boolean. Then the form returns true or false based on the boolean:

$(document).ready(function() {
    $('form').submit(function() {
        var should_return = false;
        $.ajax({
            async: false,
            url: 'test.php',
            type: 'post',
            success: function() {
                // Do some type of check
                should_return = true;
            }
        });
        return should_return;
    });
});
BenMills