I want to submit comments via ajax. CLick the comment submit button, comments gets posted via jquery's ajax method and the result ("Awaiting moderation...") is displayed on the page.
I went into the scripts.js file and added a click handler for the button. It validates and then does an ajax submit but this is not working. Clicking to submit a comment still redirects to the wp-comments-post.php page.
What's the secret to getting this to work?
Here is what I have in scripts.js:
$("#my_comment_form").validate({
submitHandler: function(data){
$.ajax({
type: "POST",
url: "../wp-comments-post.php",
data: data,
success: function(){
$( ".form_success" ).dialog( "open" );
},
error: function(){
console.log('oh snap!');
},
complete: function(){
console.log('complete');
}
});
},
errorContainer: ".errors",
rules: {
email: {
email: true
}
}
});
EDIT: My problem stemmed from the fact that the data being passed back was not complete or had errors. This caused the submitHandler to error which then let the built-in action (built into Wordpress' comment generation code) fire. By passing in correct data in the first place and handling any errors that may arise in the submitHandler's error method I was able to get things working with the above code.