Mitch Evans
Mitch Evans

Reputation: 641

Submitting a form with ajax after jQuery validation is successfull

Ok so I have a form that I am validating with jQuery Validation and I am trying to now submit it with AJAX. With the code below, once the form is valid and you click on submit the page reloads and the inputs are placed in the url address bar (i.e. method="get")

In the ajax method I have that set to POST but it doesn't appear to be using the ajax call.

What did I do wrong?

$().ready(function() {
    var $form = $(this);
   //validate the inquiry form on keyup and submit
    $("#inquiryForm").validate({
        showErrors: function(errorMap, errorList) {
            for (var error in errorMap)  {
                $.growl.error({ message: errorMap[error] });
             }
        },
        onkeyup: false,
        rules: {
            fullName: {
                required: true
            },
            email: {
                required: true,
                email: true
            },
            inquiry: {
                required: true,
                minlength: 5,
                maxlength: 500
            }
        },
        messages: {
            fullName: "Your name is required",
            email: "A valid email address is required",
            inquiry: "Your inquiry is required and must have between 5-500 characters"
        },

        submitHandler: function(form) {
            $.ajax({
                url: form_submit/inquiry_form/inquiry_form.php,
                type: "POST",
                data: $(form).serialize(),
                success: function(response) {
                    $('#inquiryFormHolder').html("Your form was submitted!");
                }            
            });
            return false;
        }
    });
});

Upvotes: 2

Views: 19532

Answers (1)

Fouad Fodail
Fouad Fodail

Reputation: 2643

Try to use :

submitHandler: function(form) {
    $.ajax({
        url: form_submit/inquiry_form/inquiry_form.php,
        type: "POST",
        data: $(form).serialize(),
        success: function(response) {
            $('#inquiryFormHolder').html("Your form was submitted!");
        }            
    });
    $form.submit();
}

Upvotes: 8

Related Questions