0

Hi i am trying to make a comment box for a webiste using only php (without database) almost succeed. But, the comments are repeatedly posting again and again for every page reload. How to fix it ?

My codes in comment.php

<form action="comment.php" method="post">
<label for="name">Name:</label><br/>
<input type="text" name="yourname"><br>
<label for="name">Comment:</label> <br/>
<textarea name="comment" id="comment" cols="30" rows="10"></textarea><br/>
<input type="submit" value="submit">
</form>

<?php
$yourname = $_POST['yourname'];
$comment = $_POST['comment'];
$data = $yourname . "<br>" . $comment . "<br><br>";
$myfile = fopen("comment.txt", "a"); 
fwrite($myfile, $data); 
fclose($myfile);
$myfile = fopen("comment.txt", "r");
echo fread($myfile,filesize("comment.txt"));
?>

Expected Output,

When user enter name and comment and submit, it have to Post a comment. (While reload it should not repeat the last posted comment again)

The output am getting,

When user enter name and comment and submit, it post the comment. But, When reload/refresh that page it post the last comment again. If once again reloaded, again posting the last comment. it repeats for everytime the page reloads.

Kindly help me fix my code. It will be much helpful. Thank You.

Machine
  • 5
  • 1

2 Answers2

0

First time learning PHP. Good for you. Though maybe spend the time better and learn Python. Anyway you have 2 things happening here.

One is that everytime the user hits the page, the php block executes regardless of if any information has been sent. You want to wrap your php code in an if statement such as:

if( count($_POST) )
{
 $yourname = $_POST['yourname'];
 $comment = $_POST['comment'];
 $data = $yourname . "<br>" . $comment . "<br><br>";
 $myfile = fopen("comment.txt", "a"); 
 fwrite($myfile, $data); 
 fclose($myfile);
 $myfile = fopen("comment.txt", "r");
 echo fread($myfile,filesize("comment.txt"));
}

Your second problem is that, once you have POSTed something, then every time you reload the page (via F5) not as in reloading from a fresh session, you need to clear the POST array. There's a lot of ways to do this, I think best for you is to stick this after that echo:

foreach( $_POST as $key=>$val )
{
   unset( $_POST[$key] );
}

See this link for more - [Unset post variables after form submission

Good luck!

Dan Miller
  • 261
  • 2
  • 7
0

You can use PRG Pattern to avoid multiple submissions.

First of all, check if the request method is POST. If so, save the comment and then redirect back (or any other page you want):

<?php
$myfile = fopen('comment.txt', 'a');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $yourname = $_POST['yourname'];
    $comment = $_POST['comment'];
    $data = $yourname . "<br>" . $comment . "<br><br>"; 
    fwrite($myfile, $data); 
    fclose($myfile);
    header('Location: comment.php');
    die();
}

$myfile = fopen('comment.txt', 'r');
echo fread($myfile, filesize('comment.txt'));
?>
N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47