评论 - 回复系统会在没有刷新页面的情况下成功发布评论,但不会回复

时间:2013-08-12 11:11:11

标签: php jquery

我创建了一个完美的php,jquery评论 - 回复系统。它像facebook一样工作。用户发布评论,然后用户可以添加回复。每次用户发表评论时,Jquery用于查看评论而不刷新页面。虽然我这样做并且与评论一起工作,但它不依赖于依赖。为了查看回复,我必须刷新页面。知道如何解决这个问题吗?

这是我的主页,wall.php:

<?php
<script> 
 $(document).ready(function(){                          
  $("#comment_process").click(function(){
    if($("#comment_text").val() != ""){ 
        $.post("comments.php?action=post", { comment: $("#comment_text").val() }, function(data) {
            $(".comments").html(data);
            $("#comment_text").val("");
         });
       } 
     });   
   });   
</script>

<div class="comment_container"> <div class="comment_form">
<textarea id="comment_text"/> </textarea>
</div></div>
<div class="comments">  <?php include_once("comments.php");?>    </div>
?>

下一页名为comments.php:

<?php
 require_once('core/init.php');
<script>
$(document).ready(function(){   
$('.reply').keyup(function(e){
    if(e.keyCode == 13){
        var comments_id = $(this).attr('comments_id');
        var reply = $(this).val();
        $.post('reply.php', {"data_comments_id":comments_id, "data_reply":reply});  
        $('.reply').val('');    
      }
    });
  });
</script>

function getComments(){
$comments = "";
$sql = mysql_query("SELECT * FROM comments ORDER BY comment_date DESC ") or die (mysql_error());
if(mysql_num_rows($sql) == 0){
  $comments = " <div class='each_comment'> There are no comments ...</div> ";
}
else
{   
   while ($row= mysql_fetch_assoc($sql)) {

    $comments .= "Says : <div class='each_comment'> ".$row['comment_date']." ".$row['comment'];
    $comments .= "<input type='text' class='reply' comments_id='{$row['comments_id']}' />";

    echo "<div class='replies'>";
    $reply_query = mysql_query("SELECT reply_id, reply, reply_date FROM comments_reply WHERE comments_id='{$row['comments_id']}' ");

    while ($run_reply= mysql_fetch_assoc($reply_query)) {
      $reply_id = $run_reply['reply_id'];
      $reply = $run_reply['reply'];
      $reply_date = $run_reply['reply_date'];                               
    $comments .= "Replied: $reply_date  $reply   ";

    echo"</div>";

    $comments .= "</div>"; 
  }
 }
return $comments;  
}

function postComments($comment){
 $comment = mysql_real_escape_string(strip_tags($comment));
 if(isset($comment) && empty($comment)){
    $sql = mysql_query(" INSERT INTO `comments` (comment, comment_date) VALUES ('".$comment."', now()) ");
 }
 return getComments();
}

if((isset($_GET['action'])) && ($_GET['action'] == "post")) {
postComments($_POST['comment']);
}

echo getComments();        
?>

最后这是我的reply.php页面:

<?php  
require_once('core/init.php');

$comments_id = $_POST['data_comments_id'];
$reply = $_POST['data_reply'];

if(isset($reply) && !empty($reply)){ 
    mysql_query("INSERT INTO comments_reply VALUES ('', '$comments_id', '$reply', now()) "); 
}

?>

0 个答案:

没有答案