PHP $_POSTVariables


In PHP, the predefined $_POST variable is used to collect values from a form with method="post".


$_POST Variable

The predefined $_POST variable is used to collect values from a form with method="post".

Information sent from a form with the POST method is invisible to anyone (it is not displayed in the browser's address bar), and there is no limit on the amount of information sent.

Note:However, by default, the maximum amount of information sent via the POST method is 8 MB (can be changed by setting post_max_size in the php.ini file).

Example

form.htmlThe file code is as follows:

<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
</head>
<body>

<form action="welcome.php" method="post">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>

</body>
</html>

When the user clicks the "Submit" button, the URL looks similar to the following:

http://www.example.com/welcome.php

The "welcome.php" file can now collect form data through the $_POST variable (note that the names of the form fields automatically become the keys in the $_POST array):

欢迎 <?php echo $_POST["fname"]; ?>!<br>
你的年龄是 <?php echo $_POST["age"]; ?>  岁。

Accessing the demo through a browser is shown below:



When to use method="post"?

Information sent from a form with the POST method is invisible to anyone, and there is no limit on the amount of information sent.

However, because the variables are not displayed in the URL, you cannot bookmark the page.


PHP $_REQUEST Variable

The predefined $_REQUEST variable contains the contents of $_GET, $_POST, and $_COOKIE.

The $_REQUEST variable can be used to collect form data sent via the GET and POST methods.

Example

You can modify the "welcome.php" file to the following code, which can accept data such as $_GET, $_POST, etc.

欢迎 <?php echo $_REQUEST["fname"]; ?>!<br>
你的年龄是 <?php echo $_REQUEST["age"]; ?>  岁。
Other Extensions