【发布时间】:2016-04-15 11:34:58
【问题描述】:
我有 3 页,并使用 $_POST 方法传递值。不使用 $_GET 方法的原因是我不希望用户转到 url 并更改值并能够传递值。从头开始为 t2 和 t3 页面:
- t2 和 t3 将检查
if(isset($_POST))然后保存到会话中。 - 当 t3.php 用户点击后退按钮时,t3 不传递
$_POST数据并读取会话
在将数据从 t1 传递到 t2 和 t2 到 t3 时可以工作,但是当单击从 t3 到 t2 的返回按钮时,浏览器显示"Confirm Form Resubmission"。当用户点击返回按钮时,如何让页面读取会话数据而不是 $_POST 值?
t1.php
<html>
<body>
<form id="b">
value1: <input type="text" name="value1" id="value1">
value2: <input type="text" name="value2" id="value2">
<input type="button" onclick ="test()" value="test">
</form>
</body>
</html>
<script type="text/javascript">
function test(){
document.getElementById("b").method = "post";
document.getElementById("b").action = "t2.php";
document.getElementById("value1").value = "asd";
document.getElementById("value2").value = "zxc";
document.getElementById('b').submit();
}
</script>
t2.php
<?php
session_start();
if(isset($_POST)){
unset($_SESSION['t2']);
$_SESSION['t2']=$_POST;
}else if(isset($_SESSION['t2'])){
}else{
header("Location: http://localhost/my_project/t1.php");
}
?>
<html>
<body>
value1<?php echo $_SESSION['t2']["value1"]; ?>!<br>
value2 <?php echo $_SESSION['t2']["value2"]; ?>.
<form id="b">
value1: <input type="text" name="value1" id="value1">
value2: <input type="text" name="value2" id="value12">
<input type="button" onclick ="test()" value="test">
</form>
</body>
</html>
<script type="text/javascript">
function test(){
document.getElementById("b").method = "post";
document.getElementById("b").action = "t3.php";
document.getElementById("value1").value = "qwe";
document.getElementById("value2").value = "rty";
document.getElementById('b').submit();
}
</script>
t3.php
<?php
session_start();
if(isset($_POST)){
unset($_SESSION['t3']);
$_SESSION['t3']=$_POST;
}else if(isset($_SESSION['t3'])){
}else{
header("Location: http://localhost/my_project/t1.php");
}
?>
<html>
<body>
value1<?php echo $_SESSION['t3']["value1"]; ?>!<br>
value2 <?php echo $_SESSION['t3']["value2"]; ?>.
</body>
</html>
【问题讨论】:
-
您在发布数据之前检查会话数据?
-
您可以在 t3.php 上设置一个会话变量,然后检查该变量是否存在于 t2.php 上。如果是这样,用户点击后退按钮,您就可以阅读会话。这是一种选择,如果您不能在使用发布数据之前先直接从会话中读取数据。
-
@Epodax,是的,因为如果用户键入从 t1 到 t2 的新数据,会话应该存储最新的值
-
@CharlotteDunois,当页面 t1 到 t2 时,我已经保存了 $_SESSION['t2'] 中的值,为什么我还需要再次存储变量?当用户单击返回按钮时,t2 页面未读取会话,不确定是不是我的代码错误