-1

I have a single page comment php web page that writes comments to a file. But on refresh it keeps reposting last comments. How do code it to only write to the file only if submit button pressed. Thank you.

<?php
    /*if($_POST)*/
    if(isset($_POST['submit_btn'])) {
        $name = $_POST['name'];
        $comment = $_POST['comment'];
        $handle = fopen("comments.php", "a");

        fwrite($handle, "<div><b><i>" . $name . "</b></i> update:<br>" . $comment . "<br></div><br>" );
        fclose($handle);
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Rolling Log</title>
        <meta charset="uft-8">
    </head>
    <style>
        body {
          background-color: grey;
        }

        #top,#bottom {
          margin: 0 auto;
          width: 50%;
          padding: 5px;
        }

        div {
          border: 1px solid black;
          background-color: white;
        }
    </style>

    <body>
        <div id="top">
            <h1>Post a change mgmt comment</h1>

            <form action="" method="POST">
                Name: <br /> <input type="text" name="name"> <br />
                Comment: <br /> <textarea rows="5" cols="70" name="comment"></textarea> <br /><br />
                <input type="submit" name="submit_btn" value="Post comment">
            </form>
        </div>
        <br>

        <div id="bottom">
            <h1>Other CM Comments</h1>
            <?php include "comments.php"; ?>
        </div>
    </body>
</html>
treyBake
  • 6,440
  • 6
  • 26
  • 57
martinb
  • 135
  • 1
  • 3
  • 13
  • But you are actually doing that. You only write to the file when the button is pressed but then you reuse the same file. So whatever is written is not deleted after you refresh the page and you just reuse it. – pr1nc3 May 31 '19 at 07:59
  • create an ajax request to handle it – treyBake May 31 '19 at 07:59
  • https://en.wikipedia.org/wiki/Post/Redirect/Get _is_ the established pattern. – 04FS May 31 '19 at 08:00
  • 1
    Possible duplicate of [Prevent form redirect OR refresh on submit?](https://stackoverflow.com/questions/1263852/prevent-form-redirect-or-refresh-on-submit) – madalinivascu May 31 '19 at 08:08

2 Answers2

0

Use the header refresh function : header("Refresh:0");

This is because when you send a request to the same page, the headers won't change (and will keep the current request to add data in your file) if you don't force them with the header function.

 /*if($_POST)*/
    if(isset($_POST['submit_btn']))
    {
        $name = $_POST['name'];
        $comment = $_POST['comment'];
        $handle = fopen("comments.php", "a");
        fwrite($handle, "<div><b><i>" . $name . "</b></i> update:<br>" . $comment . "<br></div><br>" );
        fclose($handle);
        header("Refresh:0");
    }
tcj
  • 1,645
  • 4
  • 13
  • 21
0

You have to remember somehow that form was submitted already. You can save that information in cookie. When form is submitted set some cookie and to if condition that checks if submit button is pressed check also for cookie value:

 /*if($_POST)*/
    if(isset($_POST['submit_btn']) && !isset($_COOKIE['submitted']))
    {
        setcookie('submitted');
        $name = $_POST['name'];
        $comment = $_POST['comment'];
        $handle = fopen("comments.php", "a");
        fwrite($handle, "<div><b><i>" . $name . "</b></i> update:<br>" . $comment . "<br></div><br>" );
        fclose($handle);
    }else{
// Form page refreshed - do something else
    }

Or store to php session if you prefer - pretty similar solution.

MilanG
  • 6,994
  • 2
  • 35
  • 64