Advertisement
w0lfiesmith

Comment postback for ajax commenting

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