页面刷新时PHP会话清除 - 根据输入显示和隐藏

时间:2018-03-08 21:11:22

标签: php session cookies

所以我有两个页面,两个页面都有一个输入字段,如果用户填写输入字段并转到下一页,他们将在表格的位置看到新内容。所以它基本上就像"填写此表单并显示新内容"。无论您是填写A页还是B页上的表格,另一页都会识别您是否填写了表格。有意义吗?

只要您不刷新页面或访问除了单击提交输入之外的其他页面,我就可以使用它。

我在这里做错了什么?



<!--- Page A -->

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport">
	<meta content="noindex, nofollow" name="robots">
	<title>Page A</title>
	<meta content="Page" name="description">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

			
	<form action="/b.php" id="l-form" method="post" name="l-form">
		<div class="wrapper localize" style="<?php if(isset($_POST['treasure'])) { echo "display:none;"; } else { echo "display:block;"; } ?>">
		    <div class="header_container">
		      <h2>Make sure we offer service at your location</h2>
		    </div>		      
		    <div class="input_container">
		          <input autocomplete="off" class="input-box street" id="street" name="userLogin" placeholder="Street address" tabindex="0" type="text">
		          <input autocomplete="off" class="input-box suite" id="suite" name="suite" placeholder="Suite (Optional)" tabindex="0" type="text">
		          <input autocomplete="off" class="input-box zipcode" id="zipcode" name="zipcode" placeholder="Zip code" tabindex="0" type="text">
		          <input type="submit" name="treasure" value="See offers">
		      </div>
		  </div>
		    <!-- results 3 -->
		    <div class="results" style="<?php if(isset($_POST['treasure'])) { echo "display:block;"; } else { echo "display:none;"; } ?>">
		      <p class="results-heading">Excellent news. We know who you are.</p
		    </div>
	</form>
		
	

</body>
</html>

<!--- Page B -->

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport">
	<meta content="noindex, nofollow" name="robots">
	<title>Page A</title>
	<meta content="Page" name="description">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

			
	<form action="/a.php" id="l-form" method="post" name="l-form">
		<div class="wrapper localize" style="<?php if(isset($_POST['treasure'])) { echo "display:none;"; } else { echo "display:block;"; } ?>">
		    <div class="header_container">
		      <h2>Make sure we offer service at your location</h2>
		    </div>		      
		    <div class="input_container">
		          <input autocomplete="off" class="input-box street" id="street" name="userLogin" placeholder="Street address" tabindex="0" type="text">
		          <input autocomplete="off" class="input-box suite" id="suite" name="suite" placeholder="Suite (Optional)" tabindex="0" type="text">
		          <input autocomplete="off" class="input-box zipcode" id="zipcode" name="zipcode" placeholder="Zip code" tabindex="0" type="text">
		          <input type="submit" name="treasure" value="See offers">
		      </div>
		  </div>
		    <!-- results 3 -->
		    <div class="results" style="<?php if(isset($_POST['treasure'])) { echo "display:block;"; } else { echo "display:none;"; } ?>">
		      <p class="results-heading">Excellent news. We know who you are.</p
		    </div>
	</form>
		
	

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您没有在任何地方使用会话变量。当有人提交您的表单时,您需要执行以下操作:

$_SESSION['treasure'] = $_POST['treasure'];

设置'treasure'会话变量。在另一页或刷新后,您可以访问$ _SESSION ['treasure']。

目前,当您单击submt时,这似乎有效,因为您正在为目标页面提供$ _POST ['treasure'],而不是因为您正在按预期使用会话。