php method to obtain form submission: 1. Use the predefined variable "$_GET" to obtain get data; 2. Use the predefined variable "$_POST" to obtain post data; 3. Use the predefined variable The variable "$_REQUEST" can be obtained by get or post data.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php method to obtain form submission data There are three types:
get method can use the predefined variable $_GET to obtain;
post method can use the predefined variable $ _POST to obtain;
If you don’t know which way the form submits data, you can use the predefined variable $_REQUEST to obtain it, and the data can be obtained in both ways.
1. Use the predefined variable $_GET to quickly obtain form data (the form needs to be set to method="get"
)
During the development process of the program, since the data submitted by the GET method is attached to the URL and sent, information of the type "Parameters passed by the URL user" will be displayed in the address bar of the URL, as shown below:
http://url?name1=value1&name2=value2 ...
name1 and name2 are the names of the form elements (set by the name attribute of the form elements), and value1 and value2 are the values of the form elements. The URL and form elements are separated by "?
", and multiple form elements are separated by "&
". The format of each form element is "name= value", fixed.
Let’s add the form of the user.html file and look at the address bar of the URL
You can directly use the predefined variable $_GET in the user.php file to
obtain data. The $_GET global variable is an associative array. The key of the array is the value of the form element name. The value of the array is the value of the corresponding form. (Note that all parameters in the URL can be obtained using $_GET.)
<?php var_dump($_GET); ?>
You can use the $_GET['key name']
method To get the value of each form element one by one:
<?php header("content-type:text/html;charset=utf-8"); echo "用户名为:".$_GET[&#39;user&#39;]."<br>生日为:".$_GET[&#39;bday&#39;]; ?>
2. Use the predefined variable $_POST to quickly get the form data (the form needs to be set to method="post"
)
The post method does not depend on the URL and does not display the passed parameter value in the address bar.
In addition, the POST method can transfer data to the server without restrictions. All submitted information is transmitted in the background. The user cannot see this process on the browser side, and the security is relatively high. Therefore, the POST method is more suitable for sending some important information (such as credit card number) or a relatively large amount of data to the server.
Tip: By default, the maximum size of information sent by the POST method is 8MB, which can be changed later by modifying the value of post_max_size in the php.ini configuration file.
$_POST
The global variable is also an associative array. The key name of the array is the value of the form element name, and the value of the array is the value of the corresponding form. In the actual program development process, when the method attribute of the form specifies the use of POST for data transmission, we should use $_POST to obtain the form data.
$_POST The use of global variables is the same as that of ordinary arrays. You can directly access the data in the form of $_POST['key value']
. Here is a simple example to demonstrate one time.
<?php header("content-type:text/html;charset=utf-8"); echo "用户名为:".$_POST[&#39;user&#39;]."<br>生日为:".$_POST[&#39;bday&#39;]; ?>
3. Use the predefined variable $_REQUEST to quickly obtain form data
In the actual development process, we don’t know the form What should I do when the form submits data? PHP provides a $_REQUEST global variable, which is an array containing $_POST, $_GET and $_COOKIE. The array structure is similar to $_POST and $_GET.
That is to say, whether the data is submitted using POST or GET, you can use $_REQUEST to obtain it, and you can even use $_REQUEST to obtain COOKIE information. The request_order attribute was introduced in PHP5.3. We can control the content contained in $_REQUEST by modifying the value of the request_order attribute in the php.ini configuration file.
request_order 属性的值可以是 G、P 和 C 这三个大写的英文字母,它们分别代表 GET、POST 和 COOKIE。默认情况下 request_order 属性的默认值为request_order="GP",并不包含 C,也就是说 $_REQUEST 中并不包含 COOKIE 的信息,如果我们希望 $_REQUEST 中包含 COOKIE 的话,需要将其修改为request_order="GPC"。
示例:
<?php header("content-type:text/html;charset=utf-8"); var_dump($_REQUEST); echo "用户名为:".$_REQUEST[&#39;user&#39;]."<br>生日为:".$_REQUEST[&#39;bday&#39;]; ?>
推荐学习:《PHP视频教程》
The above is the detailed content of What are the ways to get form submission in php. For more information, please follow other related articles on the PHP Chinese website!