PHP页面如何检索表单数据? [英] How does a PHP page retrieve form data?

查看:89
本文介绍了PHP页面如何检索表单数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,正在W3Schools上PHP课程.在本课程的表单处理部分中,我读到有两种处理表单数据的方法:POSTGET.我知道,根据所使用的方法,会创建一个超全局变量,该变量将数据存储为键值对.在目标页上,从此变量中检索数据(使用$_POST["key"]$_GET["key"]).

I am new to PHP and am taking the PHP course on W3Schools. In the form handling part of the course, I read that there are two methods of handling form data : POST and GET. I understand that depending on the method used, a superglobal variable is created which stores the data as key-value pairs. And at the destination page, the data is retrieved from this variable (using $_POST["key"] or $_GET["key"]).

但是我的问题是:超全局变量($_POST$_GET)存储在哪里?如果它在表单所在的同一页面上,那么如何通过form标记的action属性中指定的另一个目标页面进行访问?

But my question is : Where is the superglobal variable ($_POST or $_GET) stored? If it is on that same page on which the form exists, how can it be accessed by another destination page specified in the action attribute of the form tag?

服务器上的所有页面是否都可以访问相同的这些超全局变量集(与我目前认为的每个页面具有自己的那些设置相反)?

Is it that the same set of these superglobal variables are accessible by all the pages on the server (contrary to my present belief that each page has its own set of those)?

下面的代码应该使我的问题更清楚:

The code below should make my question more clear:

index.php:

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:

welcome.php:

<html>
<body>

    Welcome <?php echo $_POST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

W3Schools解释如下:

W3Schools explains as follows :

当用户填写上面的表单并单击提交"按钮时,表单数据将发送到一个名为"welcome.php"的PHP文件中进行处理.表单数据使用HTTP POST方法发送. 要显示提交的数据,您可以简单地回显所有变量.

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.

How do the variables $_POST["name"] and $_POST["email"] reach welcome.php.

推荐答案

当您将请求发送到Web服务器(即apache,nginx)时,该请求将被解析为易于理解的片段.然后,对于PHP,将请求的文件与请求中的数据一起发送到PHP解释器.

When you send a request to a web server (i.e. apache, nginx), the request is parsed into pieces to be understood. Then, in PHP's case, the file requested is sent to the PHP interpreter with the data from the request.

在您的示例中,您希望表单将POST请求发送到页面welcome.php. (如果您不熟悉GET/POST/etc方法,那么w3schools对此有一个简单的解释: http://www.w3schools.com/tags/ref_httpmethods.asp )您的表单的方法设置为post,可以为get,但通常不会.如果用户按下<form>标记内的提交按钮,则表单使用method中设置的方法将请求发送到action中设置的页面.您的表单将发送一个POST请求,其数据名为="..."和电子邮件="...".

In your example, you want a form to send a POST request to the page welcome.php. (If you're new to requests methods GET/POST/etc, then w3schools has a simple explanation on that: http://www.w3schools.com/tags/ref_httpmethods.asp) Your form has the method set to post, this can be get but it generally never is. If a user pressed a submit button within a <form> tag, then the form sends to the request to the page set in action using the method set in method. Your form will send a POST request with the data name="..." and email="...".

此请求到达PHP时,发送给它的所有数据将存储在方法全局变量中.所有发布请求将其数据放置在$ _POST中,所有获取请求将其数据放置在$ _GET中.

When this request reaches PHP, all data sent to it will be stored in the method global variable. All post request place their data in $_POST, all get requests place their data in $_GET.

因此,当您提交表单POST请求时,浏览器(Firefox/Chrome)将使用方法POST请求页面welcome.php,并在该请求中发送数据nameemail .在您的服务器上,PHP将打开文件welcome.php并将数据放入$ _POST数组.然后,由于$ _POST是全局的,因此welcome.php中的任何地方都可以访问此变量.

So when you submit your forms POST request, the page welcome.php will be requested by your browser (Firefox/Chrome) using the method POST and sending the data name and email in that request. On your server, PHP will open the file welcome.php and place the data into the $_POST array. Then, since $_POST is global, anywhere within welcome.php will have access to this variable.

如果您手动转到页面welcome.php,则将执行GET请求.通常,发送帖子请求的唯一方法是通过表单.因此,手动转到该页面会将$ _POST设置为一个空数组.

If you manually go to the page welcome.php, you will be performing a GET request. Generally, only way to send a post request is through a form. So manually going to that page will have $_POST set to an empty array.

在学习PHP时,我几乎总是在页面顶部显示这些行,所以我知道发生了什么事情:

When I was learning PHP, I almost always had these lines at the top of the page so I knew what was going on:

<pre><?php print_r($_GET);?></pre>
<pre><?php print_r($_POST);?></pre>

这将以漂亮的格式打印出数组,并且<pre>标签将确保在浏览器中正确格式化它们(因为print_r使用换行符,并且浏览器会忽略这些字符,除非在pre标签中).

This will print out the arrays in a pretty format, and the <pre> tags will make sure they're formatted correctly in the browser (since print_r uses newline characters, and browsers ignore those unless in pre tags).

您也可以将print_rvar_dump交换,以获取有关该变量的更多信息.

You could also swap print_r with var_dump to get way more info about the variable.

这是一个简单的答案,Web技术还有更多的方法.并且,一旦您对PHP有了足够的了解,我就会进入CakePHP或CodeIgniter之类的框架,这些框架会带走很多东西.不过,重要的是要先了解基础知识.

This is a simple answer, there's way more to web technologies. And once you understand PHP enough I'd move into frameworks like CakePHP or CodeIgniter which takes away a lot of the little bits. It's important to understand the basics first though.

这篇关于PHP页面如何检索表单数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆