Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i am trying to do a comment system with ajax and it works perfectly but i am trying to do validation with it because i do not want it to submit if the value of hte form is empty and i cannot figure what the problem is.
The follow ins the ajax code :

 $(document).ready(function(){

   $(document).on('click','.submitComment',function(e) {

        e.preventDefault();
        //send ajax request
        var form = $(this).closest('form');
        var formValue = $(this).closest('form').val();
        if (formValue.length > 0)
        {
        $.ajax({
            url: 'ajax_comment.php',
            type: 'POST',
            cache: false,
            dataType: 'json',
            data: $(form).serialize(), //form serialize data
            beforeSend: function(){
                //Changeing submit button value text and disableing it
                $(this).val('Submiting ....').attr('disabled', 'disabled');
            },
            success: function(data)
            {
                var item = $(data.html).hide().fadeIn(800);
                $('.comment-block_' + data.id).append(item);

                // reset form and button
                $(form).trigger('reset');
                $(this).val('Submit').removeAttr('disabled');
            },
            error: function(e)
            {
                alert(e);
            }
        });
    }
    else
    {
        alert("Please fill in the form");

    }
    });
});

It just keep going to the else part of the condistion even when the form is filled up.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900