Skip to content

Instantly share code, notes, and snippets.

@fikrirasyid
Created March 12, 2015 02:15
Show Gist options
  • Save fikrirasyid/83e67de47feeb8fc8ba6 to your computer and use it in GitHub Desktop.
Save fikrirasyid/83e67de47feeb8fc8ba6 to your computer and use it in GitHub Desktop.
Ajaxifying Comment - Send Comment as AJAX Call
jQuery(document).ready(function($){
/**
* Ajaxify Comment Submission
* Assuming that comment form's ID is #commentform. Adjust it as you see fit
*/
$('body').on('submit', '#commentform', function(e){
// Stop the default form behavior
e.preventDefault();
var commentform = $(this);
var action = commentform.attr( 'action' );
var inputs = commentform.serializeArray();
var submitting_comment = $('#submitting-comment');
// Submitting comment
commentform.ajaxSubmit({
beforeSend: function(){
// Display the loading state
commentform.find('p').slideUp();
submitting_comment.slideDown();
},
success: function( responseText, statusText, xhr, form ) {
// Switch the existing comment area with the comment area returned from AJAX call
var page = $(responseText);
var comments = page.find('.comments-area' );
$('.comments-area').replaceWith( comments );
},
error: function( xhr, textStatus, errorThrown ){
// Translates the error code status into understandable error message
if( textStatus == 'error' ){
var error_code = xhr.status;
alert( simple_ajax_comment_params.error_messages[error_code] );
// Unloading state
commentform.find('p').slideDown();
submitting_comment.slideUp();
}
},
url : action,
clearForm : true,
data : inputs
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment