附加数据并写注释—注释重复


append data and write comment the comment repeated

有问题

当添加3个帖子并在帖子上写一条评论时,2号评论重复。

澄清更多

当我添加3个帖子

post 1=

后2=b

后3=c

并在帖子2上写一条评论,这条评论重复请求帮助

像这张照片

这是文件

index.php

<? include("../post/includes/config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <script type="text/javascript" src="jquery.js"></script>
      <script type="text/javascript">
        $(function(){
          $("#update_state").submit(function(){
            var text = $(".text") .val();
            var s = {
              "text":text
            }
            $.ajax({
              url:'action.php',
              type:'POST',
              data:s,
              cache: false,
              beforeSend: function (){
                $(".loading") .show();
                $(".loading") .html("loading...");
              },
              success:function(html){
                $("#show_new_posts") .prepend(html);
                $(".loading") .hide();
                $(".text") .val("");
              }
            });
          return false;
          });
          //add comment
          $(".comment_form").submit(function(){
            var id_post = $(this).attr('rel');
            var text_comm  = $(".commtext[rel="+id_post+"]") .val();
            var s = {
              "id_post":id_post,
              "text_comm":text_comm
            }
            $.ajax({
              url:'add_comment.php',
              type:'post',
              data:s,
              beforeSend: function (){
                $(".loadingcomm[rel="+id_post+"]") .show();
                $(".loadingcomm[rel="+id_post+"]") .html("<img src='"style/img/ajax/load1.gif'" alt='"Loading ....'" />");
              },
              success:function(html){
                $(".loadingcomm[rel="+id_post+"]").hide();
                $(".commtext[rel="+id_post+"]") .val("");
                $(".shownewcomm[rel="+id_post+"]").append(html);
              }
            });
          return false;
        });
      });
    </script>
  </head>
  <body>
    <form id="update_state">
      <textarea class="text" name="text"></textarea>
      <input type="submit" value="go" />
    </form>
    <div class="loading"></div>
    <div id="show_new_posts"></div>
<?
$select_posts = $mysqli->query("SELECT * FROM posts ORDER BY id DESC LIMIT  4 ");
$num_posts = $select_posts->num_rows;
if($num_posts){
  while ($rows_posts = $select_posts->fetch_array(MYSQL_ASSOC)){
    $id_posts             = $rows_posts ['id'];
    $post_posts           = $rows_posts ['post'];
?>
<div style="padding: 5px; margin: 5px; background:#ccc; width:300px;">
  <b>admin</b>
</div>
<div style="padding: 5px; margin: 5px; background:#ccc; width:300px;">
  <? echo $post_posts; ?>
</div>
<div class="shownewcomm" rel="<? echo $id_posts; ?>"></div>
<form rel="<? echo $id_posts; ?>" class="comment_form">
  <textarea rel="<? echo $id_posts; ?>" style="height: 20px;" placeholder="Comment.." class="commtext"></textarea>
  <input type="submit" value="comment" />
</form>
<hr />
<?
}
}
?>
</body>
</html>

action.php

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $(function(){
    $(".comment_form").submit(function(){
      var id_post = $(this).attr('rel');
      var text_comm  = $(".commtext[rel="+id_post+"]") .val();
      var s = {
        "id_post":id_post,
        "text_comm":text_comm
      }
      $.ajax({
        url:'add_comment.php',
        type:'post',
        data:s,
        beforeSend: function (){
          $(".loadingcomm[rel="+id_post+"]") .show();
          $(".loadingcomm[rel="+id_post+"]") .html("<img src='"style/img/ajax/load1.gif'" alt='"Loading ....'" />");
        },
        success:function(html){
          $(".loadingcomm[rel="+id_post+"]").hide();
          $(".commtext[rel="+id_post+"]") .val("");
          $(".shownewcomm[rel="+id_post+"]").append(html);
        }
      });
      return false;
    });
  });
</script>

<?php
include("../post/includes/config.php");
$text = $_POST['text'];
$insert = $mysqli->query("INSERT INTO posts (id, post) VALUE ('', '$text')");
$id_posts = $mysqli->insert_id;
if($insert){
  ?>
<div style="padding: 5px; margin: 5px; background:#ccc; width:300px;">
  <b>admin</b>
</div>
<div style="padding: 5px; margin: 5px; background:#ccc; width:300px;">
  <? echo $text; ?>
</div>
<div class="shownewcomm" rel="<? echo $id_posts; ?>"></div>
<form rel="<? echo $id_posts; ?>" class="comment_form">
  <textarea rel="<? echo $id_posts; ?>" style="height: 20px;" placeholder="Comment.." class="commtext"></textarea>
  <input type="submit" value="comment" />
</form>
<hr />
<?
}
?>

add_comment.php

<?php
$id_post = $_POST['id_post'];
$text_comm = $_POST['text_comm'];
?>
admin :  <? echo $text_comm;  ?>
<br />

index.phpaction.php中有$(".comment_form").submitajax call。在index.php中,您准备的action.php结果如下

$("#show_new_posts").prepend(html);

可能正因为如此,才会有不止一个ajax call。从action.php中删除所有JavaScript代码并尝试

更新:

action.php中所做的更改如下:

  1. 从中删除了jquery引用,因为index.php中已经存在,并在末尾移动了其他JS代码

  2. 以形式添加id

    <form rel="<?php echo $id_posts; ?>" id="comment_form_<?php echo $id_posts; ?>" class="comment_form">

  3. 更新提交处理程序以使用如下表单id:

    $("#comment_form_<?php echo $id_posts; ?>").submit(function(){

更新后的action.php代码如下:

<?php
include("../post/includes/config.php");
$text = $_POST['text'];
$insert = $mysqli->query("INSERT INTO posts (id, post) VALUE ('', '$text')");
$id_posts = $mysqli->insert_id;
if ($insert) {
    ?>
    <div style="padding: 5px; margin: 5px; background:#ccc; width:300px;">
        <b>admin</b>
    </div>
    <div style="padding: 5px; margin: 5px; background:#ccc; width:300px;">
        <?php echo $text; ?>
    </div>
    <div class="shownewcomm" rel="<?php echo $id_posts; ?>"></div>
    <form rel="<?php echo $id_posts; ?>" id="comment_form_<?php echo $id_posts; ?>" class="comment_form">
        <textarea rel="<?php echo $id_posts; ?>" style="height: 20px;" placeholder="Comment.." class="commtext"></textarea>
        <input type="submit" value="comment" />
    </form>
    <hr />
    <script type="text/javascript">
        $(function() {
            $("#comment_form_<?php echo $id_posts; ?>").submit(function(){
                var id_post = $(this).attr('rel');
                var text_comm = $(".commtext[rel=" + id_post + "]").val();
                var s = {
                    "id_post": id_post,
                    "text_comm": text_comm
                }
                $.ajax({
                    url: 'add_comment.php',
                    type: 'post',
                    data: s,
                    beforeSend: function() {
                        $(".loadingcomm[rel=" + id_post + "]").show();
                        $(".loadingcomm[rel=" + id_post + "]").html("<img src='"style/img/ajax/load1.gif'" alt='"Loading ....'" />");
                    },
                    success: function(html) {
                        $(".loadingcomm[rel=" + id_post + "]").hide();
                        $(".commtext[rel=" + id_post + "]").val("");
                        $(".shownewcomm[rel=" + id_post + "]").append(html);
                    }
                });
                return false;
            });
        });
    </script>
    <?php
}
?>