0

I have made a very basic form using php that takes input and then displays it. Once I have typed my text in the input field and submitted, I reload the page. After reloading, my text is still being displayed when it should not have been displayed because of the if conditional containing the isset() function. Steps to verify my problem:

  1. Go here.
  2. Enter any thing in the input area and click submit.
  3. You should see your input displayed just below the input area.
  4. Refresh the page.
  5. You will still see your previously entered text below the input area.
  6. And this is exactly my problem. I want the form to refresh after refreshing the page.
<!DOCTYPE html>
<html>
    <body>
        <form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>">
            <input type="text" name="name">
            <input type="submit">
        </form>
        <?php
            if (isset($_POST["name"])){
                echo $_POST["name"];
            }
        ?>
    </body>
</html>
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • There's no input area at that link. – Barmar Feb 17 '22 at 18:09
  • 1
    https://en.wikipedia.org/wiki/Post/Redirect/Get is the solution here. Your site is down, but I'll bet your browser says "are you sure you want to re-submit this form" in a message when you refresh. – ceejayoz Feb 17 '22 at 18:09
  • Your problem is not being reproduced – Dev Man Feb 17 '22 at 18:10
  • _Refresh the page_ Should give you a clue. If you place your cursor in the address bar and hit enter, I bet it wont still be there – RiggsFolly Feb 17 '22 at 18:10
  • You refresh a page in which you submited a post data and that data is part of the refresh. Most browsers even give you a warning about this when your refresh ("do you want to submit the data again?"). What you describe is the exact expected result – Zeitounator Feb 17 '22 at 18:11
  • "You will still see your previously entered text below the input area." Yes, that's exactly what your code looks like it's meant to be doing?? – Nicolas Goosen Feb 17 '22 at 18:11
  • Just delete the PHP echo under the form? – Nicolas Goosen Feb 17 '22 at 18:12
  • @NicolasGoosen i think OP means that value persists in the input field after submission even after a refresh – Dev Man Feb 17 '22 at 18:12
  • Also what is the problem, Please provide us with what do you want exactly – Dev Man Feb 17 '22 at 18:13
  • @DevMan that's what I thought too at first, but it's not - reread his post: "You will still see your previously entered text below the input area... And this is exactly my problem." – Nicolas Goosen Feb 17 '22 at 18:13
  • @NicolasGoosen yeah my bad reread it , this question is a bit confusing isnt it? – Dev Man Feb 17 '22 at 18:14
  • @DevMan Ah, sorry! Now I see - it's after refreshing - you were right! Yes, confusing... – Nicolas Goosen Feb 17 '22 at 18:16

0 Answers0