下面的代码是一个php表单,写入一个txt文件并在同一页输出结果。php表单写入文本文件 - 防止POST重定向重新提交
但是,当我刷新页面POST数据被自动添加 - 我想要防止这种情况。我明白我应该将用户重定向到另一个页面,但我怎么做,如果我的结果是在同一页上,我希望他们留在同一页上:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Score Watch</title>
</head>
<body>
<p>
<form id="form1" name="form1" method="post" action="">
<label>Date:
<input name="date" class="datepicker" id="date" required type="text"
data-hint="" value="<?php echo date('d/m/Y'); ?>" />
</label>
<br>
<label>Score:
<input type="text" name="name" id="name" />
</label>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
</p>
<?php
$date = $_POST["date"];
$name = $_POST["name"];
$posts = file_get_contents("posts.txt");
$posts = "$date $name<br>" . $posts;
file_put_contents("posts.txt", $posts);
echo $posts;
?>
</body>
</html>
我见过的header('Location')
战术,但我不能使用它,因为我在同一页上输出数据?
保存数据会话,或通过获取方法传递它 –