我是 PHP 新手,正在 W3Schools 上 PHP 类(class).在类(class)的表单处理部分,我读到有两种处理表单数据的方法:POST和 GET .我知道根据使用的方法,创建了一个超全局变量,它将数据存储为键值对。在目标页面,数据从此变量中检索(使用 $_POST["key"]
或 $_GET["key"]
)。
但我的问题是:超全局变量($_POST
或$_GET
)存储在哪里?如果它在表单存在的同一页面上,它如何被 form
标记的 action
属性中指定的另一个目标页面访问?
服务器上的所有页面是否都可以访问同一组超全局变量(这与我目前认为每个页面都有自己的一组变量相反)?
下面的代码应该让我的问题更清楚:
文件 index.php
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
文件 welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
W3Schools解释如下:
When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. To display the submitted data you could simply echo all the variables.
变量 $_POST["name"]
和 $_POST["email"]
如何到达 welcome.php?
最佳答案
Superglobals不是指变量的生命周期,而是指变量可以被访问的范围。
Session variables可以被其他页面访问。
此外,关于您的评论“如果是这样的话,second.php 如何显示在 first.php 中输入的表单数据?”
first.php 应该将数据保存到数据库中。 second.php 应该从数据库中读取数据。如果您是新手,我建议您将 MySQL 作为数据库。它与 PHP 配合得很好。
关于php - PHP 页面如何检索表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34685637/