Home  >  Article  >  Backend Development  >  Summarize the method of receiving form data in php

Summarize the method of receiving form data in php

PHPz
PHPzOriginal
2023-04-04 09:14:312612browse

PHP is a very powerful server-side scripting language that is widely used in web development. In web development, receiving form data is a very important step, and PHP provides a variety of methods for receiving form data. Let’s take a look at these methods together.

1. Basic form submission method

First of all, the most basic method of submitting a form is to submit it through the

tag in HTML. In the tag, there is an attribute called "method", the default value is "get", you can also use "post" to submit. So what is the difference between these two submission methods?

(1) Get method: Attach the form data to the URL and pass it. The data can be seen in the address bar of the browser. Since the length of the URL is limited, it is suitable for passing shorter data, such as search keywords, etc. The code for receiving form data using the get method is as follows:

<?php
echo $_GET[&#39;name&#39;];
?>

(2) Post method: Pass the form data as part of the HTTP package, and the data will not appear in the address bar. Since the data is transferred through HTTP packets, it is suitable for transferring larger data, such as uploading files, etc. The code for receiving form data using the post method is as follows:

<?php
echo $_POST[&#39;name&#39;];
?>

2. $_GET and $_POST arrays

We can receive form data through $_GET and $_POST arrays. Among them, $_GET is used to receive data submitted by the get method, and $_POST is used to receive data submitted by the post method. Both arrays are PHP superglobal variables and can be accessed from anywhere.

Take receiving a form data named "name" as an example. When the form is submitted using the get method, the PHP code that receives the data is as follows:

<?php
echo $_GET[&#39;name&#39;];
?>

When the form is submitted using the post method, the PHP code that receives the data is as follows:

``
< ?php
echo $_POST['name'];
?>
``

If we don’t know the name of the form data, we can use PHP’s foreach statement to output all submissions The data is as follows:

<?php
foreach ($_POST as $key => $value) {
    echo $key . ':' . $value;
}
?>

3. $_REQUEST array

In addition to the $_GET and $_POST arrays, PHP also provides a global array $_REQUEST. The $_REQUEST array can receive any data submitted using the get or post method, and there is no need to care about which method is used to submit the data. As long as the submitted data has a name, it can be accessed through the $_REQUEST array.

Take receiving a form data named "name" as an example. When submitting a form using the get method or post method, the PHP code to receive the data is as follows:

<?php
echo $_REQUEST[&#39;name&#39;];
?>

It should be noted that the value in the $_REQUEST array may be overwritten by the $_GET or $_POST array, so if there is Duplicate data will be accessed according to the value of $_GET or $_POST.

4. $_SERVER['QUERY_STRING'] variable

In addition to using the $_GET and $_POST arrays to receive form data, we can also use a special variable $_SERVER['QUERY_STRING provided by PHP '] to get the query string in the URL. The query string is the part after "?" in the URL, which includes the parameters used by the website to query data.

For example, for the following URL:

http://example.com/index.php?id=1&page=2

We can get the id and page parameters as follows:

<?php
$query_string = $_SERVER[&#39;QUERY_STRING&#39;];
echo $query_string; // 输出:id=1&page=2
?>

It should be noted that $_SERVER['QUERY_STRING '] variable can only get the query string part of the URL, but cannot get the POST data.

Summary

The above is how PHP receives form data. By using PHP functions such as $_GET, $_POST, $_REQUEST, $_SERVER['QUERY_STRING'], etc., we can easily receive and process form data. Next time you develop a web application, don't forget to choose the method that best suits you for receiving form data.

The above is the detailed content of Summarize the method of receiving form data in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn