0

I have code that gets called whenever the form is submitted, this then validates the form via ajax. Now if the validation is successful, how do I submit the form

$('#homeSubscriptionFromSub').submit(function(){    
                var formData = $(this).serialize();
                var formUrl = '/subscriptions/subscribe';

                $.ajax({
                        type: 'POST',
                        url: formUrl,
                        data: formData,
                        success: function(data,textStatus,xhr){
                                if(data == ''){
                                    return true;
                                }else{
                                    $("#subscriptionForm").html(data);
                                }
                        },
                        error: function(xhr,textStatus,error){
                                alert(textStatus);
                        }
                });
                return false;
            });

What I do in the above code, I validate the form server side, now when data is empty it means that it passed the validation test, how can I submit the form when data is empty, I tried return true but this is not working

3 Answers 3

1

The AJAX call is asynchroneous. Use the submit() method (without arguments) to submit the form. Also, I recommend to use event.preventDefault() to cancel a form submissipn beforehand.

To not cancel your new request by the same function, a variable is created, to keep track whether the form is valid or not.

(function(){
    var valid = false;
    $('#homeSubscriptionFromSub').submit(function(e){    
        if(valid){
            valid = false;
            return; //Don't prevent default
        }
        e.preventDefault();
        var formData = $(this).serialize();
        var formUrl = '/subscriptions/subscribe';

        $.ajax({
                type: 'POST',
                url: formUrl,
                data: formData,
                success: function(data,textStatus,xhr){
                        if(data == ''){
                            valid = true;
                            $('#homeSubscriptionFromSub').submit();
                        }else{
                            $("#subscriptionForm").html(data);
                        }
                },
                error: function(xhr,textStatus,error){
                        alert(textStatus);
                }
        });
        return false;
    });
})();
Sign up to request clarification or add additional context in comments.

2 Comments

I tried $(this).submit(); and this did not work, I will try using the id
@Roland I have included the full recommended code in my answer. Just calling $(..).submit() will not work, because the function cancels each submit event. This behaviour has to be dealt with, as shown in the suggested code.
1

AJAX is asynchronous. Your function will return false; before the $.ajax finishes. You need to do your request synchronously, or trigger the form's submit event when the AJAX function completes.

You will probably have to unbind the validation function first though, or you'll simply wind up recursively validating your form on submit.

Comments

1

You must return true in order for it to submit, you are returning false since your using ajax.

$('#homeSubscriptionFromSub').submit(function(){    
                var formData = $(this).serialize();
                var formUrl = '/subscriptions/subscribe';

                $.ajax({
                        type: 'POST',
                        url: formUrl,
                        data: formData,
                        success: function(data,textStatus,xhr){
                                if(data == ''){
                                    return true;
                                }else{
                                    $("#subscriptionForm").html(data);
                                    this.submit();
                                }
                        },
                        error: function(xhr,textStatus,error){
                                alert(textStatus);
                        }
                });
                return false;
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.