如何获取发布的数据

时间:2013-08-03 18:08:39

标签: php post

我是php的新手,所以也许我的问题很简单。假设我有以下内容

的index.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<form name="quiz" method="post" action="includes/process.php">
First Name: <input type="text" name="firstname" id="fname"/><span>*</span>
<p></p>
Last Name: <input type="text" name="lastname" id="lname"/><span>*</span>
<input type="submit" name="submit" value="Go"></input>
</form>
</body>
</html>

我希望将数据发布到下一页:

process.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Form Post</title>
<body>
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname']; 
echo "my name is $firstname<br/>";
echo "my lastname is $lastname<br/>";
?>
</body>
</html>

现在我的问题是如何再次获取process.php的数据并将它们发布到另一个php页面或者将它们保存在mysql数据库中?有没有关于如何获取或使用发布数据的教程?

1 个答案:

答案 0 :(得分:1)

您可以使用会话来存储数据。

<?php
session_start();
if (isset($_POST['firstname'], $_POST['lastname'])) {
// Store session data
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
}
?>

然后再次获取数据。

<?php
session_start();
if (!isset($_SESSION)) {
// Get session data
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
echo $firstname;
echo $lastname;
}
?>

或者将其与链接一起传递

<a href="nextpage.php?firstname=<?php echo $firstname ?>&lastname=<?php echo $lastname ?>" >nextpage</a> 

然后再次获取数据

<?php
if (isset($_GET['firstname'], $_GET['lastname'])) {
// Get variables passed with link
$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];
echo $firstname;
echo $lastname;
}
?>