同一评论显示在多个php网页上

时间:2019-02-05 13:51:40

标签: javascript php

我有多个文章网页,并且在每篇文章上,我都在文章下方放置了一个评论系统,以便人们可以评论有关该文章的任何内容。

问题在于,每当我对任何文章发表评论时,它都会向我显示对其他文章的相同评论。

所以人们请帮助我。在此先感谢。

 
      <!--Javascript of Comment System -->
<script>
$(document).ready(function(){
 
 $('#comment_form').on('submit', function(event){
  event.preventDefault();
  var form_data = $(this).serialize();
  $.ajax({
   url:"add_comment.php",
   method:"POST",
   data:form_data,
   dataType:"JSON",
   success:function(data)
   {
    if(data.error != '')
    {
     $('#comment_form')[0].reset();
     $('#comment_message').html(data.error);
     $('#comment_id').val('0');
     load_comment();
    }
   }
  })
 });

 load_comment();

 function load_comment()
 {
  $.ajax({
   url:"fetch_comment.php",
   method:"POST",
   success:function(data)
   {
    $('#display_comment').html(data);
   }
  })
 }
  
 $(document).on('click', '.reply', function(){
  var comment_id = $(this).attr("id");
  $('#comment_id').val(comment_id);
  $('#comment_name').focus();
 });
 
});
</script>
	<!--comment system script and css stylesheet-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    <!--This is the comment container-->
<p class="container"><h3>Ask A Question</h3></p>
<br>

    <!--This is the comment system-->
  <form method="POST" id="comment_form">
  <div class="form-group">
  <input type="text" name="comment_name" id="comment_name" class="form-control" placeholder="Enter Name" />
    </div>
    <div class="form-group">
     <textarea name="comment_content" id="comment_content" class="form-control" placeholder="Enter Comment" rows="5" ></textarea>
    </div>
    <div class="form-group">
     <input type="hidden" name="comment_id" id="comment_id" value="0" />
     <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
    </div>
   </form>
   <span id="comment_message"></span>
   <br />
   <div id="display_comment"></div>
  </div>

</div>

fetch_comment.php

add_comment.php

1 个答案:

答案 0 :(得分:1)

查看您的load_comment函数:

 function load_comment()
 {
     $.ajax({
         url:"fetch_comment.php",
         method:"POST",
         success:function(data)
         {
             $('#display_comment').html(data);
         }
     })
 }

没有引用文章ID。本质上,您是在发送评论请求,而没有指定评论应该来自哪篇文章。现在,假设您通过添加对article_id的请求来解决此问题:

 function load_comment()
 {
     $.ajax({
         url:"fetch_comment.php",
         data: {article_id: 0},
         method:"POST",
         success:function(data)
         {
             $('#display_comment').html(data);
         }
     })
 }

结果您仍然会收到相同的评论。这是因为您的fetch_comment.php脚本没有能够通过article_id指定注释的功能。查看fetch_comment.php的前几行:

$connect = new PDO('mysql:host=localhost;dbname=techotub_testing', 'techotub_testing', 'password');

$query = "
    SELECT * FROM tbl_comment 
    WHERE parent_comment_id = '0' 
    ORDER BY comment_id DESC
";

$statement = $connect->prepare($query);

$statement->execute();

在这里我们可以看到您正在请求tbl_comment的所有评论,而没有指定文章。要获得特定文章的所有评论,您要做的是在where子句中添加一行以指定您所请求的文章。如果通过ajax通过post调用传递article_id,则可以使用$_POST['article_id']

不幸的是,由于插入了SQL语句,您将无法使用任何类型的文章过滤功能:

$query = "
 INSERT INTO tbl_comment 
 (parent_comment_id, comment, comment_sender_name) 
 VALUES (:parent_comment_id, :comment, :comment_sender_name)
 ";

这告诉我们,添加新评论时,您没有在表上保存文章ID。如果要按文章过滤,则需要有一个文章列,并在每个插入内容上指定ID。完成后,您应该可以开始编辑其余代码以按文章进行过滤。

tldr

  1. 在表中添加商品ID列
  2. 在添加新评论时开始将商品ID值插入表格中
  3. 在select子句中添加新行,以按文章ID显示
  4. 将文章ID从ajax函数传递到php脚本
相关问题