-1

我有个问题

当添加 3 个帖子并在帖子编号 2 上重复发表评论时写评论。

澄清更多

当我添加 3 个帖子时

帖子 1 = 一个

帖子 2 = b

帖子 3 = c

并在帖子 2 上发表评论,此评论重复请求帮助

喜欢这张照片

这是文件

索引.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>

动作.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 />
4

1 回答 1

0

$(".comment_form").submitajax callinindex.phpaction.php。并且在index.php你前面添加这样的action.php结果

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

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

更新:

所做的更改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
}
?>
于 2013-07-14T15:03:25.157 回答