附加数据并写评论重复评论

时间:2013-07-14 14:28:12

标签: php jquery

我遇到了问题

当添加3个帖子并对帖子编号2评论重复写评论时。

澄清更多

当我添加3个帖子

发布1 = a

帖子2 = b

帖子3 = c

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

像这样photo

这是文件

的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 />

1 个答案:

答案 0 :(得分:0)

$(".comment_form").submitajax call index.php action.php index.php。在action.php中,您正在预先$("#show_new_posts").prepend(html); 结果

ajax call

可能因此会有多个JavaScript。从action.php删除所有action.php代码,然后尝试

更新

index.php所做的更改如下:

  1. JS中删除了jquery引用,并在末尾移动了其他<form rel="<?php echo $id_posts; ?>" id="comment_form_<?php echo $id_posts; ?>" class="comment_form">代码

  2. 在表单中添加了ID

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

  3. 更新了提交处理程序以使用表单ID,如下所示:

    action.php

  4. 更新的<?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 } ?> 代码如下:

    {{1}}