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