0

我是 PHP 新手,我正在尝试在提交表单后获取发布数据。我遇到的问题是我可以在我的 success.php 页面上获取帖子数据,但是如果我离开该页面并转到另一个页面,我将无法再获取这些值。所以我的问题是如何在另一个页面上获取帖子数据?

我的表格

<form name="message" method="post" action="success.php">
First Name: <input type="text" name="firstname" id="firstname"/>
<p>
Last Name: <input type="text" name="lastname" id="lastname"/>
<p>
Message:<br />
<textarea name="message" id="message" cols="45" rows="5"></textarea>
<br />
<input type="submit" name="submit" value="Submit"></input>
</form>

成功.php

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$message = $_POST['message'];
echo "Firstname: $firstname<br/>";
echo "Lastname: $lastname<br/>";
echo "Message $message";
?>
4

3 回答 3

0

在您的表单提交到success.php 之后,您可以将值存储在会话中。

<?php
if (isset($_POST['firstname'], $_POST['lastname'], $_POST['message'])) {
session_start();
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname']; 
$message = $_POST['message'];

// store session data
$_SESSION['firstname'] = $firstname;
$_SESSION['lastname'] = $lastname;
$_SESSION['message'] = $message;
}
?>

然后在您可以使用的其他页面上获取数据。

<?php
if (!isset($_SESSION)) {
session_start();
}
if (isset($_SESSION['firstname']) &&($_SESSION['lastname']) &&($_SESSION['message'])){
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$message = $_SESSION['message'];
echo $firstname;
echo $lastname;
echo $message;
}
?>
于 2013-10-03T15:25:09.667 回答
0

几乎给出了相同的答案,但澄清了:

您可以将这些值存储在会话数组中。

$_SESSION['example'] = "testone";
$_SESSION['exampletwo'] = "testtwo";

然后检查特定变量或遍历当前变量:

$example;
if (isSet($_SESSION['example'])) {
    $example = $_SESSION['example'];
} else {
    $example = "default";
}
echo "example's content: $example";

echo "Session array contents:";
echo "<pre>";
var_dump($_SESSION);
echo "</pre>";
echo "Session from for loop:"
for ($_SESSION as $name => $value) {
    echo "$name => $value";
}
于 2013-10-03T15:40:46.460 回答
-2

在会话中存储价值

    <?php
session_start();
    $firstname = $_SESSION['firstname'];
    $lastname = $_SESSION['lastname'];
    $message = $_SESSION['message'];
    ?>
于 2013-10-03T15:27:00.277 回答