0

I've this simple PHP code ....

<html>
 <head>
 </head>
 <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;

     header('Location: '.$_SERVER['REQUEST_URI']);
     exit();
    }
  ?>
 </body>
</html>

I've added ...

     header('Location: '.$_SERVER['REQUEST_URI']);
     exit();

.. as suggested here Avoiding form resubmit in php when pressing f5 or here Simple Post-Redirect-Get code example to manage the page refresh, otherwise a message comes out asking me confirm to resend and repeat the action and does not to clear the page.

I've tried both using 'REQUEST_URI' or 'PHP_SELF' ... the result is that the page is refreshed and cleaned BUT I can't see in my page the result of the echo "The typed city is: ".$city; instruction ...

I'd like to can see the city name typed and then, when I refresh the page, clean all avoiding any message on refresh

Suggestions? Thank you in advance

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
    After redirecting there is no `$city` variable. You need to store the value into session/cookie when you need it to show on another page. – pavel Mar 17 '18 at 13:27
  • simple; you're outputting before header; remove the echo above header. – Funk Forty Niner Mar 17 '18 at 13:28
  • I need to show the city name before to refresh and AFTER, when I refresh the page clean all so the user can re-type another string – Cesare Mar 17 '18 at 13:32
  • Just Remove the ` header('Location: '.$_SERVER['REQUEST_URI']);` and `exit`. because you already submit same page so you don't want to redirect – Nawin Mar 17 '18 at 13:37
  • but if I remove these two rows when I refesh the page comes out a bowser message that says ... `to display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier`. – Cesare Mar 17 '18 at 14:27

0 Answers0