评论框中的评论是重写,而不是发布

时间:2017-07-22 06:11:29

标签: javascript php html css html5

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

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

3 个答案:

答案 0 :(得分:0)

你真的是这个意思吗? '您的电子邮件地址不会被公开。'您将所有“邮件地址”写入明文文件以便大声喊叫。不要把它放在网上。

答案 1 :(得分:0)

您没有回读文件来绘制评论,您只是使用了来自的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>
PHP中的

$_POST指的是脚本响应的POST请求的主体。

您需要将其替换为执行以下三项操作的功能:

  1. 从文件中读取。
  2. 使用authorcomment
  3. 将记录拆分为不同的条目
  4. 迭代这些条目并为每条记录绘制一个单独的div。
  5. 另一个问题:您的代码正在将当前日期打印为评论的日期。如果您没有存储评论的日期,则可能不应显示日期。

答案 2 :(得分:-1)

<?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帖子中的网址(第26行)