Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use this code to post comments with ajax on my WP blog. It works fine but I display the comment form on my homepage so the plugin only works for the first post (first comment form).

I want it to work for all the forms.

Here is my code:

if (typeof jQuery != 'undefined') {
  jQuery('document').ready(function($){
    var commentform=$('#commentform');
    commentform.prepend('<div id="comment-status" ></div>');

    var statusdiv=$('#comment-status');

    commentform.submit(function(){
      var formdata=commentform.serialize();
      statusdiv.html('<p>Processing...</p>');

      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 comment. We appreciate your response.</p>');
          else
            statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
            commentform.find('textarea[name=comment]').val('');
        }
      });

      return false; 
    });
  });
}
share|improve this question
    
var commentform=$('#commentform'); - this line seems to be a problem. As ID,s should be unique on a page, You should use class instead, to "plug" into all forms, and then iterate each. –  Marcin Bobowski Feb 15 '13 at 0:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.