-2

when I hit the post button, comment is posting on the page. If I hit it again, it overriding the previous comment. can someone please clarify this? And I'm running java script to display the date. It is showing all the time without any comments also. How can I modify that too? Thanks in advance!

<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>
Necvetanov
  • 374
  • 3
  • 15
Naresh
  • 1
  • I do not understand something, are you saving the comments into a file, and not a DB ? – Treast Jul 22 '17 at 06:13
  • you write to a file but you never read from it. Instead you are just writing the latest comment. – Garr Godfrey Jul 22 '17 at 06:19
  • Where is your form closing? – Ankit Singh Jul 22 '17 at 06:29
  • See my answer below. The world already has to much spam. This code isn't safe enough to put online especially when you are responsible for e-mail addresses that are not your own. You should handle them if they were your own and keep them safe and secure. – Digital Human Jul 22 '17 at 06:43

3 Answers3

0

Whay do you mean with; 'Your email address will not be published.' You write all 'mail addresses' to a plaintext file for crying out loud. DONT PUT THIS ONLINE.

Digital Human
  • 1,599
  • 1
  • 16
  • 26
0

You're not reading the file back to paint the comments, you are just using the POST data that came in:

<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 in PHP refers to the body of the POST request that the script is responding to.

You need to replace that with functionality that does three things:

  1. Reads from the file.
  2. Splits the records into different entries with an author and comment.
  3. Iterates over those entries and paints a separate div for each record.

Another gotcha: Your code is printing the current date as the date of the comment. If you are not storing the date of the comments, you probably shouldn't show the date.

Ezra Chu
  • 832
  • 4
  • 13
-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>

don't forget to change url in ajax post with your php filename (line 26)