Advertisement
w0lfiesmith

Wordpress/jQuery AJAX comment post

Mar 31st, 2012
2,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.23 KB | None | 0 0
  1. jQuery('document').ready(function($){
  2.     var commentform=$('#commentform'); // find the comment form
  3.     commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors
  4.     var statusdiv=$('#comment-status'); // define the infopanel
  5.    
  6.     commentform.submit(function(){
  7.             //serialize and store form data in a variable
  8.             var formdata=commentform.serialize();
  9.             //Add a status message
  10.             statusdiv.html('<p>Processing...</p>');
  11.             //Extract action URL from commentform
  12.             var formurl=commentform.attr('action');
  13.             //Post Form with data
  14.             $.ajax({
  15.                 type: 'post',
  16.                 url: formurl,
  17.                 data: formdata,
  18.                 error: function(XMLHttpRequest, textStatus, errorThrown){
  19.                     statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
  20.                 },
  21.                 success: function(data, textStatus){
  22.                     if(data=="success")
  23.                         statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
  24.                     else
  25.                         statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
  26.                     commentform.find('textarea[name=comment]').val('');
  27.                 }
  28.             });
  29.             return false;
  30.        
  31.     });
  32. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement