Clear form data if the page refresh?

0

I am running a simple test and would like to know if there is any way to clear the form data if the user gives refresh to the browser.

<?php
$test1 = "";
$test2 = "";
if($_SERVER["REQUEST_METHOD"] == "POST")
{   $test1 = $_POST['test1'];
    $test2 = $_POST['test2'];
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <input type="text" name="test1" value="<?php echo $test1;?>"/>
    <input type="text" name="test2" value="<?php echo $test2;?>"/>
    <input type="submit" name="btn_sub" value="Enviar" />
</form>

Whenever I type something in the fields if I give refresh in the browser the data remains. I would like to know if you can clear these fields by clicking on the browser refresh.

    
asked by anonymous 14.02.2017 / 21:09

1 answer

2

Change the request headers with header and tell the browser not to save anything in cache :

<?php
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$test1 = "";
$test2 = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $test1 = $_POST['test1'];
    $test2 = $_POST['test2'];
}?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <input type="text" name="test1" value="<?php echo $test1;?>"/>
    <input type="text" name="test2" value="<?php echo $test2;?>"/>
    <input type="submit" name="btn_sub" value="Enviar" />
</form>

Source: How to control web page caching, across all browsers? .

    
14.02.2017 / 23:39