提交表格后如何获得发布数据

时间:2013-10-03 15:23:29

标签: php html

我是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>

success.php

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

3 个答案:

答案 0 :(得分: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;
}
?>

答案 1 :(得分: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";
}

答案 2 :(得分:-2)

会议中的商店价值

    <?php
session_start();
    $firstname = $_SESSION['firstname'];
    $lastname = $_SESSION['lastname'];
    $message = $_SESSION['message'];
    ?>