2014-02-24 107 views
1

我試圖用註釋本教程Ajaxify WordPress Comments如何實現AJAX在WordPress

在這裏,在WordPress的ajaxify「評論是我的PHP處理程序:

function ajaxify_comments($comment_ID, $comment_status){ 
    if(! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
     //If AJAX Request Then 
     switch($comment_status) { 
      case '0': 
       //notify moderator of unapproved comment 
       wp_notify_moderator($comment_ID); 
      case '1': //Approved comment 
       echo "success"; 
       $commentdata = &get_comment($comment_ID, ARRAY_A); 
       $post = &get_post($commentdata['comment_post_ID']); 
       wp_notify_postauthor($comment_ID, $commentdata['comment_type']); 
       break; 
      default: 
       echo "error"; 
     } 
     exit; 
    } 
} 
add_action('comment_post', 'ajaxify_comments', 20, 2); 

這裏是我的腳本:

jQuery('document').ready(function($){ 
    var commentform=$('#commentform'); // find the comment form 
    commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors 
    var statusdiv=$('#comment-status'); // define the infopanel 

    commentform.submit(function(){ 
     //serialize and store form data in a variable 
     var formdata=commentform.serialize(); 
     //Add a status message 
     statusdiv.html('<p>Processing...</p>'); 
     //Extract action URL from commentform 
     var formurl=commentform.attr('action'); 
     //Post Form with data 
     $.ajax({ 
      type: 'post', 
      url: formurl, 
      data: formdata, 
      error: function(XMLHttpRequest, textStatus, errorThrown){ 
       statusdiv.html('<p class="ajax-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; 
     }); 
}); 

我每次發表評論時,都會得到:「請稍等片刻,然後再發表您的下一條評論」。希望有人能告訴我我做錯了什麼?

+0

請告訴我數據的價值和textStatus當你嘗試了嗎?放一些'console.log(data); console.log(textStatus);'裏面的成功。 – gonzalon

+0

剛剛做完了... console.log(data);和console.log(textStatus);是成功 – henrywright

+0

如果是這樣,它應該爲「感謝您的評論,我們感謝您的迴應。」信息!你看到數據庫中的評論嗎? – gonzalon

回答

2

試試這個:

jQuery('document').ready(function($){ 
    var commentform=$('#commentform'); // find the comment form 
    commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors 
    var statusdiv=$('#comment-status'); // define the infopanel 

    commentform.submit(function(){ 
     //serialize and store form data in a variable 
     var formdata=commentform.serialize(); 
     //Add a status message 
     statusdiv.html('<p>Processing...</p>'); 
     //Extract action URL from commentform 
     var formurl=commentform.attr('action'); 
     //Post Form with data 
     $.ajax({ 
      type: 'post', 
      url: formurl, 
      data: formdata, 
      error: function(XMLHttpRequest, textStatus, errorThrown) 
       { 
        statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>'); 
       }, 
      success: function(data, textStatus){ 
       if(data == "success" || textStatus == "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; 
    }); 
}); 
+0

太棒了!我現在得到:「感謝您的評論,我們感謝您的回覆。」此外,我可以看到評論是在數據庫中,但問題是評論文字不顯示在屏幕上,直到我做手動頁面刷新 – henrywright

+2

@ gonzalo-naveira你應該也給一個解釋,不僅複製/粘貼+編輯代碼。 這個網站應該互相教/學,不是「誰會做我的功課?」網站。 – Dragos

+0

@Dragos我們已經在評論中討論過了。請參閱上文。 – henrywright