我有一个页面检查$ _POST是否已设置,然后将发布数据保存到会话变量$ _SESSION。页面重定向时,它不会在结果页面上显示会话数据。当我执行print_r($ _ SESSION)时,它显示一个空数组。在每个页面上,我都包含一个名为include.php的文件。在该页面中,执行以下操作
<?php session_start(); ?>
index.php
<?php
include 'assets/include.php';
require 'assets/dbinfo.php';
$query = "SELECT * FROM Authors ORDER BY first_name";
$resultObj = $connection->query($query);
if(count($_POST))
{
$_SESSION['formPostData'] = $_POST;
header('Location: final.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Fundamentals</title>
<link href="assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Header">
<img src="assets/Dickens_Gurney_head.jpg" border="0" alt="">
<h2>
Join Our Literature Mailing List
</h2>
</div>
<div id="Body">
<form method="post" action="index.php" >
<div>
<label>Favorite Author:</label>
<select name="author">
<?php while($row = $resultObj->fetch_assoc()): ?>
<option value="<?=$row['id']?>"><?=$row['first_name']?> <?=$row['last_name']?></option>
<?php endWhile; ?>
</select>
</div>
<div class="multiple">
<label>Favorite Century:</label>
17th Century <input type="checkbox" name="century[]" value="17th">
18th Century <input type="checkbox" name="century[]" value="18th">
19th Century <input type="checkbox" name="century[]" value="19th">
</div>
<div>
<label>Comments:</label>
<textarea name="comments"></textarea>
</div>
<div>
<label>Name:</label>
<input type="text" name="name" />
</div>
<div>
<label>E-mail Address:</label>
<input type="text" name="email" />
</div>
<div class="multiple">
<label>Receive Newsletter:</label>
Yes <input type="radio" name="newsletter" value="yes">
No <input type="radio" name="newsletter" value="no">
</div>
<div class="multiple">
<label> </label>
<input type="submit" name="submit" value="Join Mailing List">
</div>
</form>
</div>
</body>
</html>
final.php
<?php
include 'assets/include.php';
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
$postedData = $_SESSION['formPostData'];
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Fundamentals</title>
<link href="assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="Header">
<img src="assets/Dickens_Gurney_head.jpg" border="0" alt="">
<h2>
Mailing List Information
</h2>
</div>
<div id="Body">
<div>
<label>Favorite Author:</label>
<span><?=$postedData['author']?> </span>
</div>
<div>
<label>Favorite Century:</label>
<span><?=$postedData['century']?> </span>
</div>
<div>
<label>Comments:</label>
<span><?=$postedData['comments']?> </span>
</div>
<div>
<label>Name:</label>
<span><?=$postedData['name']?> </span>
</div>
<div>
<label>E-mail Address:</label>
<span><?=$postedData['email']?> </span>
</div>
<div>
<label>Receive Newsletter:</label>
<span><?=$postedData['newsletter']?> </span>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
您的表单似乎直接发布到final.php
,因此您应该能够访问$_POST
中发布的数据。
由于index.php
中的代码未运行(表单发布到另一个文件),因此不会将其设置为会话
如果要将数据设置到会话中,则应在final.php
中进行设置。
答案 1 :(得分:0)
按照index.php中的表单操作,您的表单将发布到final.php,在此处您未设置任何会话数据,因此会话数据为空数组。
如果您希望将会话数据打印在final.php中,那么在设置会话数据后必须将表单操作更改为index.php,您需要将其重定向到final.php 然后,您将获得会话数据。