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
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