0

I've some trouble with PHP code using form when refesh the page.

I've tried to prepare a code sample to show the problem ...

<html>
    <body>
        <form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
            City name: <input type="text" name="city"><br>
            <input type="submit" name="submit" value="Search"><br>
        </form>

        <?php
        if(isset($_POST['submit']))
        {
            $city = $_POST['city'];

            echo "The typed city is: ".$city;
        }
        ?>
    </body>
</html>

All works fine but if you try to refresh the page (or Ctrl+F5 in the browser ... ), a message comes out asking me confirm to resend and repeat the action and does not to clear the page

enter image description here

I've seen that there are some other question on this stuff, but I'm not able to adopt them ... any help / example?

EDIT

I've solved changing my original code in this way (starting form there ... https://www.canbike.org/information-technology/php-form-clear-data-on-refresh-or-back-button-press.html)

First file: NoRefreshForm.php

<?php
session_start();
$_SESSION['page1']=1;
?>

<html>
<body>
    <form name="test" action="NoRefreshCalculate.php" method="post">
        City name: <input type="text" name="city"><br>
        <input type="submit" name="submit" value="Search" />
    </form>
</body>
</html>

Second file: NoRefreshCalculate.php

<?php
session_start();
?>

<html>
<body>
    <?php
        if (isset($_POST['city']) && isset($_SESSION['page1']))
        {
            #Do calculation here. Store in $_SESSION.
            $_SESSION['page2']="2";

            $_SESSION['city']=$_POST['city'];
            header('Location: NoRefreshResults.php');
    }
        else
        {
            header('Location: NoRefreshForm.php');

        }
    ?>
</body>
</html>

Third file: NoRefreshResults

<?php
session_start();
?>

<html>
<body>
  <form action="NoRefreshCalculate.php" method="post">
    City name: <input type="text" name="city"><br>
    <input type="submit" name="submit" value="Search" />
  </form>

    <?php
        if (isset($_SESSION['page2']))
        {
            # echo results
            if (isset($_SESSION['city'])) {
              echo "City name = ".$_SESSION['city'];
            }

            session_destroy();
            session_start();
            $_SESSION['page1']=1;
        }
        else
        {
            header('Location: NoRefreshForm.php');
        }
    ?>
</body>
</html>

In this way you can submit text, elaborate it and refresh the page without browser message on reload. I hope this helps someone with the same problem

Cesare
  • 1,629
  • 9
  • 30
  • 72
  • 1
    For a simple search, it could be more easy to use the `get` method. It depends of the purpose. Otherwise, you could store the posted values (in session, by example) and reload the page using `header("Location: url")`. – Syscall Mar 03 '18 at 17:53
  • some example on how to use `header("Location: url")`? – Cesare Mar 03 '18 at 17:55
  • 1
    `header("Location: /url/to/page");exit;` Where `/url/to/page` could be your `$_SERVER['PHP_SELF']`. See http://php.net/manual/en/function.header.php – Syscall Mar 03 '18 at 17:59

0 Answers0