Php 追加数据并编写注释重复注释

Php 追加数据并编写注释重复注释,php,jquery,Php,Jquery,我有个问题 当添加3篇文章并在第2篇文章上写评论时,重复评论 进一步澄清 当我添加3个帖子时 职位1=a 职位2=b 职位3=c 并在帖子2上写一条评论,这条评论反复请求帮助 像这样 $("#show_new_posts").prepend(html); 这是文件 index.php <? include("../post/includes/config.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi

我有个问题

当添加3篇文章并在第2篇文章上写评论时,重复评论

进一步澄清

当我添加3个帖子时

职位1=a

职位2=b

职位3=c

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

像这样

$("#show_new_posts").prepend(html);
这是文件

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>

$(函数(){
$(“#更新_状态”).submit(函数(){
var text=$(“.text”).val();
变量s={
“文本”:文本
}
$.ajax({
url:'action.php',
类型:'POST',
数据:s,
cache:false,
beforeSend:函数(){
$(“.loading”).show();
$(“.loading”).html(“loading…”);
},
成功:函数(html){
$(“显示新帖子”).prepend(html);
$(“.loading”).hide();
$(“.text”).val(“”);
}
});
返回false;
});
//添加注释
$(“.comment_form”).submit(函数(){
var id_post=$(this.attr('rel');
var text_comm=$(“.commtext[rel=“+id_post+”]”)val();
变量s={
“id_post”:id_post,
“文本通信”:文本通信
}
$.ajax({
url:'add_comment.php',
类型:'post',
数据:s,
beforeSend:函数(){
$(“.loadingcomm[rel=“+id_post+”]).show();
$(“.loadingcomm[rel=“+id_post+”]”)html(“”);
},
成功:函数(html){
$(“.loadingcomm[rel=“+id_post+”])。hide();
$(“.commtext[rel=“+id_post+”]”)val(“”);
$(“.showNewCom[rel=“+id_post+”])。附加(html);
}
});
返回false;
});
});
管理
你有
$(“.comment_form”)。在
index.php和
action.php中使用
ajax调用提交
。在
index.php
中,您正在准备
action.php
这样的结果

$("#show_new_posts").prepend(html);
可能是因为这个原因,将有多个
ajax调用
。从
action.php
中删除所有
JavaScript
代码,然后重试

更新:

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
}
?>
  • index.php中删除了jquery引用,并在末尾移动了其他
    JS
    代码

  • 在表单中添加id


    -1读了几遍之后,我仍然不知道你想说什么。我已经在代码中添加了一些缩进(如果编辑得到确认),这样您的代码至少是可读的。
    
    <?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
    }
    ?>