【问题标题】:comment from comment box is overriding, not posting [duplicate]评论框中的评论是压倒一切的,而不是发布[重复]
【发布时间】:2017-07-22 06:11:29
【问题描述】:

当我点击发布按钮时,评论正在发布在页面上。如果我再次点击它,它会覆盖之前的评论。有人可以澄清一下吗?我正在运行 java 脚本来显示日期。它一直在显示,也没有任何 cmets。我该如何修改呢? 提前致谢!

<h3 id="reply-title" class="comment-reply-title">Leave a Reply </h3> 

<form action="" method="post">

<p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>
<p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>

<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" aria-required="true" required="required"></p>

<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" aria-required="true" required="required"></p>

<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"></p>
<div>
    <?php
        if($_POST) {
            $name = $_POST['author'];
            $comment = $_POST['comment'];
            $email = $_POST['email'];
        #echo $name . $comment  . $email;
            $handle = fopen("comments/get-more-twitter-followers.html", "a");

            fwrite($handle, "\n".$name."\n".$comment."\n". $email. "\n");
            fclose($handle);

        }
    ?>
    <?php
        if($_POST) {
            $email = $_POST['email'];
            $useremail = fopen("useremail.html", "a");
            fwrite($useremail, "\n".$email. "\n");
            fclose($useremail);

        }
    ?>
</div>
<div>
    <div>
        <p><strong><h4><u>Comments:</u></h4></strong></p>
    </div>
    <div>
        <div>
            <strong><?php 
                $name = $_POST['author'];
                echo $name; ?> </strong> <small><script type="text/javascript">
                                                var d = new Date()
                                                document.write(d.getDate())
                                                document.write("/")
                                                document.write(d.getMonth() + 1)
                                                document.write("/")
                                                document.write(d.getFullYear())
                                                </script>
                            </small>
        </div>
        <div>
            <p>
                <?php
                    $comment = $_POST['comment'];
                    echo "\n".$comment."\n"."<hr>";
                ?>
            </p>
        </div>
    </div>
</div>

【问题讨论】:

  • 我有些不明白,你是把 cmets 保存到一个文件中,而不是一个 DB 中吗?
  • 您写入文件但您从未读取过它。相反,您只是在写最新的评论。
  • 您的表单在哪里关闭?
  • 在下面查看我的答案。世界已经有很多垃圾邮件。此代码不够安全,无法放到网上,尤其是当您对不属于您自己的电子邮件地址负责时。如果它们是您自己的,您应该处理它们并确保它们的安全。

标签: javascript php css html


【解决方案1】:

你是什么意思; '您的电子邮件地址不会被公开。'您将所有“邮件地址”写入纯文本文件以大声喊叫。不要把它放在网上。

【讨论】:

    【解决方案2】:

    您不是在读回文件来绘制 cmets,您只是在使用传入的 POST 数据:

    <div>
        <strong><?php 
         $name = $_POST['author'];
         echo $name; ?> </strong> <small><script type="text/javascript">
         var d = new Date()
         document.write(d.getDate())
         document.write("/")
         document.write(d.getMonth() + 1)
         document.write("/")
         document.write(d.getFullYear())
     </script>
     </small>
    </div>
    <div>
     <p><?php
     $comment = $_POST['comment'];
     echo "\n".$comment."\n"."<hr>";
     ?></p></div>
     </div></div>
    </div>
    

    $_POST 在 PHP 中指的是脚本正在响应的 POST 请求的主体。

    您需要将其替换为执行三件事的功能:

    1. 从文件中读取。
    2. 使用authorcomment 将记录拆分为不同的条目。
    3. 遍历这些条目并为每条记录绘制一个单独的 div。

    另一个问题:您的代码将当前日期打印为评论的日期。如果您不存储 cmets 的日期,则可能不应该显示日期。

    【讨论】:

      【解决方案3】:
      <?php
      $comment = $_POST['comment']; 
      $name = $_POST['author']; 
      $data_post->name = $name;
      $data_post->comment = $comment;
      
      if(isset($name, $comment)){
          header('Content-type: application/json');
          echo(json_encode($data_post));
          return;
      }
      ?>
      <html>
          <head>
              <script
                  src="https://code.jquery.com/jquery-1.12.4.min.js"
                  integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
                  crossorigin="anonymous"></script>
              <script>
                  $("document").ready(function(){
                      $("#comment").on("submit", function(e){
                          var comment = $('#comment');
                          e.preventDefault();
                          $.ajax({
                              method: "POST",
                              url: "index.php",  // your php file name
                              data: comment.serialize(),
                              success: function (data) {
                                  var d = new Date();
                                  var data_comment = "<div><small>"+d.getDate()+"/"+(d.getMonth() + 1)+"/"+d.getFullYear()+"</small></div><strong>"+data.name+"</strong><p>"+data.comment+"</p>"
                                  $("#list_comment").append(data_comment);
                                  comment[0].reset();
                                  console.log(data.name);
                              },
                              error: function (data) {
      
                              },
                          })
                      });
                  });
              </script>
          </head>
          <body>
             <h3 id="reply-title" class="comment-reply-title">Leave a Reply </h3> 
              <form action="" method="post" id="comment">
                  <p class="comment-notes"><span id="email-notes">Your email address will not be published.</span> Required fields are marked <span class="required">*</span></p>
                  <p class="comment-form-comment"><label for="comment">Comment</label> <textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>
                  <p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" aria-required="true" required="required"></p>
                  <p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" aria-required="true" required="required"></p>
                  <p class="form-submit"><input name="submit" type="submit" id="submit" class="submit" value="Post Comment"></p>
              </form>
              <div>
                  <div>
                      <p><strong><h4><u>Comments:</u></h4></strong></p>
                  </div>
                  <div id="list_comment">
      
                  <div>
              </div>
          </body>
      </html>
      

      不要忘记用你的 php 文件名更改 ajax 帖子中的 url(第 26 行)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多