下面的代碼是一個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')
戰術,但我不能使用它,因爲我在同一頁上輸出數據?
保存數據會話,或通過獲取方法傳遞它 –