0

I hereby attach my code snippets:-

// comments handler in functions.php
add_action('comment_post','my_comment_handler',20, 2);
function my_comment_handler($comment_ID,$comment_status){
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
        switch($comment_status){
            case "0":
            wp_notify_moderator($comment_ID);
            case "1": //Valid comment
            echo "success";
            $commentdata = get_comment($comment_ID, ARRAY_A);
            $post = get_post($commentdata['comment_post_ID']);
            wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
            break;
            default:
            echo "error";
        }
    exit;
    }


}

javascript that's enqueued

jQuery(document).ready( function($) {
var commentform = $('#commentform');

/* var comment = $('#comment');
var rating = $('#rating'); */
commentform.prepend('<div class="review-status"></div>');
/* $('.stars').click(function(){
    // alert ("you clicked stars");
    if($('#mystyle')){
    $('#mystyle').remove();
    }
}); */

var statusdiv =$('.review-status');
$(commentform).submit(function(e){
    e.preventDefault();

    var formdata=commentform.serialize();

    validate([$('#comment'),$('#rating'),$('#author'),$('#email'),$('#wpgdprc')]);//does basic validation to check for empty values


     //alert(formdata);
    //Add a status message
    statusdiv.html('<p>Processing...</p>');
    //Extract action URL from commentform
    var formurl=commentform.attr('action');
    $.ajax({
        type: 'post',
        url: formurl,
        data: formdata,
        error: function(XMLHttpRequest, textStatus, errorThrown){
        statusdiv.html('<p class="wdpajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');

        },
        success: function(data, textStatus){
          /*   if(data=="success")
            statusdiv.html('<p class="ajax-success" >Thanks for your Review.It will be published,once it\'s approved</p>');
            else */
            statusdiv.html('<p class="ajax-error" >Thanks for your Review.It will be published,once it\'s approved</p>');
            commentform.find('textarea[name=comment]').val('');
             $('.comment-notes').html('');

             $(':input','#commentform').not(':button, :submit, :reset, :hidden').val('').prop('checked', false) .prop('selected', false);//resetting form fields
             //$('.stars.selected ').css({'background-color':'green'});

            //  $('head').append('<style id="mystyle" type="text/css"> p.stars.selected a.active:before,p.stars.selected a:not(.active):before{opacity: 0.2;} </style>');




            },
    });
    return false;

    });
    function validate(x){
        var valid = true;
        var statusmsg='';
        $.each(x,function(index,inputId){
            if(!inputId.is(":checkbox")) {

                if(inputId.val()==''){
                    valid =false;
                    var name =  inputId.attr('name');
                    statusmsg+= '<p>Fill the '+name+'</p><br/>'; 
                }

            }
            else{
                if(!inputId.prop('checked')){
                    valid =false;
                    statusmsg+= '<p>Click the checkbox </p><br/>'; 

                    }

            }


        });
        if(valid==false){
            statusdiv.html(statusmsg);
            $('#email-notes').html('');
            exit;
        }



    }



});

closed as off-topic by Krzysiek Dróżdż, shanebp, Jacob Peattie, Christine Cooper, rudtek Jan 11 at 16:07

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.