BigJobbies
BigJobbies

Reputation: 4033

jQuery - Validation plugin submitting form in error

Im using the a jQuery validation plugin ... and trying to post the data using ajax.

The problem im finding is that once the ajax runs, it submits the default 'action' in the form tag.

The jQuery code i have is as follows, im hoping someone can help me.

    <script type="text/javascript">
        jQuery(document).ready(function() {

            var platinumForm = jQuery('#platinum-form');

            platinumForm.validate({
                rules: {
                    fullname: "required",
                    email: {
                        required: true,
                        email: true
                    }
                },
                messages: {
                    fullname: "Please enter your Fullname",
                    email: "Please enter a valid Email Address"
                },
                errorPlacement: function(error, element) {
                    error.appendTo( element.prev() );
                },
                submitHandler: function() {
                    jQuery('#platinum-form').fadeOut();

                    var iName = platinumForm.find('#fullname');

                    var data = 'fullname=' + iName.val();

                    $.ajax({
                        url: "/my-processing-page.php",
                        type: "POST",
                        data: data,
                        dataType: "html",
                        cache: false,
                        success: function(html){
                            console.log(html);
                            if (html.indexOf('Sorry') > -1) {
                                $('#paymentMessage').html(html).fadeIn('slow');
                            } else {
                                $('#form-payment-content').slideUp('slow', function(){
                                    $('#thankyouMessage .message').html(html).parent().fadeIn('slow');

                                });
                            }
                        }

                    });                     

                },
            })              
        });
    </script>

Thank you

Upvotes: 0

Views: 783

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Write return false; after $.ajax

               .......
               $.ajax({
                    url: "/my-processing-page.php",
                    type: "POST",
                    data: data,
                    dataType: "html",
                    cache: false,
                    success: function(html){
                        console.log(html);
                        if (html.indexOf('Sorry') > -1) {
                            $('#paymentMessage').html(html).fadeIn('slow');
                        } else {
                            $('#form-payment-content').slideUp('slow', function(){
                                $('#thankyouMessage .message').html(html).parent().fadeIn('slow');

                            });
                        }
                    }

                });   
                return false;
                ......  

Upvotes: 2

Related Questions