-3

I cannot make my post system work because posts in comment section duplicate if I refresh the page

I am using only HTML and PHP. It's a forum for me and my friends.

There is also code above this but it is unimportant

<form action="" method="POST">

    <label> Topic: 
    <input type="text" name="Topic" class="Input" style="width: 300px" required>
   </label>
   <br><br>
   <label> Name: 
    <input type="text" name="Name" class="Input" style="width: 225px" required>
   </label>
   <br><br>
   <label> Comment: <br>
    <textarea name="Comment" class="Input" style="width: 300px" required></textarea>
   </label>
   <br><br>
   <input type="Submit" name="Submit" value="Submit" class="Submit">


<!--idk-->

</form>
</center>
<hr>
    <br>




</body>

<!--posts-->

 </html>
<html>
 <center>
 </html>
<?php
 
 if($_POST['Submit']){
  print "<h1>Your comment has been submitted!</h1>";
 }
  ?>

<html>
</center>
</html>


<?php
  $Topic = $_POST['Topic'];
  $Name = $_POST['Name'];
  $Comment = $_POST['Comment'];

  
  #Get old comments
  $old = fopen("comments.txt", "r+t");
  $old_comments = fread($old, 1024);

  #Delete everything, write down new and old comments
  $write = fopen("comments.txt", "w+");
  $string = "<b>".$Topic."</b><br>".$Name."</b><br>".$Comment."</br>\n".$old_comments;
  fwrite($write, $string);
  fclose($write);
  fclose($old);
 

 #Read comments
 $read = fopen("comments.txt", "r+t");
 echo "<br><br>Comments<hr>".fread($read, 1024);
 fclose($read);

 ?> 
Ruli
  • 2,592
  • 12
  • 30
  • 40
  • You are not using `if($_POST['Submit']){` around your code, so it's executed on each page load. I would suggest writing this to the database btw. If it has to be a file, then use rather csv instead of a text file( https://www.php.net/manual/en/function.fputcsv.php and https://www.php.net/manual/en/function.fgetcsv.php ) – Chrostip Schaejn Feb 05 '23 at 21:57
  • And you should learn about validating and sanitizing user input before you save anything somewhere, otherwise your forum will be hacked lightning fast (https://stackoverflow.com/questions/3126072/what-are-the-best-php-input-sanitizing-functions) – Chrostip Schaejn Feb 05 '23 at 22:05
  • Note that the `
    ` element has been [obsolete](https://html.spec.whatwg.org/dev/obsolete.html#obsolete) for many, many years. In addition, it looks like your HTML in general is invalild. Fix those errors first.
    – Rob Feb 05 '23 at 23:11
  • May be you should not allow the application to refresh the page at all. That is after the post request data is written, redirect to another page confirming the success of the post operation. – prasad_ Feb 06 '23 at 05:20

1 Answers1

0

The problem you are facing is due to the way you are appending the comments to the "comments.txt" text file. The problem is that every time a comment is sent, all old and new comments are written to a text file. So when you refresh the page, the same comment is repeated.

Hope the following code helps

<?php
  $Topic = $_POST['Topic'];
  $Name = $_POST['Name'];
  $Comment = $_POST['Comment'];

  # Write the new comment to the top of the file
  $write = fopen("comments.txt", "a+");
  $string = "<b>".$Topic."</b><br>".$Name."</b><br>".$Comment."</br>\n";
  fwrite($write, $string);
  fclose($write);
 

  # Read comments from the file
  $read = fopen("comments.txt", "r");
  echo "<br><br>Comments<hr>";
  while(!feof($read)){
    $line = fgets($read);
    echo $line."<br>";
  }
  fclose($read);
?>
user580950
  • 3,558
  • 12
  • 49
  • 94
  • now it shows Parse error: syntax error, unexpected end of file in /home/code/www/index.php on line 151 but i cant see any problems with closings etc – possessor of 67 IQ Feb 05 '23 at 22:09