How to submit a form using jQuery Validate submitHandler

6.4k views Asked by At

this is jQuery submit form code. what i like to know is how can i submit the form on using submitHandler. thanks in advance.

submitHandler: function(form) {
    // do other stuff for a valid form
    $.post('thankyou.php', $("#confrom").serialize(), function(data) {
        $("#confrom").fadeOut('fast', function(){
            $('#results').html(data);
        });
    });
}
1

There are 1 answers

0
Sparky On

There is a submitHandler built into the plugin that contains something like form.submit(). Since you're over-riding that with your own custom submitHandler, you still need to use the submit. Otherwise, in your case, simply do some kind of Ajax instead of .submit().

The submitHandler goes inside of the .validate() method. In order to use the .validate() method, you must first include the jQuery Validate plugin after you include jQuery.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>

<script>
$(document).ready(function() {
    $('#yourform').validate({
        // other options,
        submitHandler: function(form) {
            // do other stuff for a valid form
            $.post('thankyou.php', $(form).serialize(), function(data) {
                $("#confrom").fadeOut('fast', function(){
                    $('#results').html(data);
                });
            });
        }
    });
});
</script>