PHP $_POST Variable
The PHP $_POST is a built-in super global variable which is always available in all scopes. It is used to collect data which is passed via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. Information sent using the POST method is invisible to others.
The data sent by POST method goes through HTTP header therefore the security depends on HTTP protocol. The secure HTTP can be used to make sure that the information is secure.
The HTTP POST method has no limits on the amount of information to send. However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
The HTML form
Consider the example below where test.php contains following code. This page will send the POST variables ("name" and "age") to the action script, greeting.php.
<html> <body> <form action="greeting.php" method="POST"> Name: <input type = "text" name = "name" /> Age: <input type = "text" name = "age" /> <input type = "submit" /> </form> </body> </html>
The test.php will look similar to:
The action script
Now, consider the greeting.php as the action script page which contains the following code:
<html> <body> <?php echo "Hello ". $_POST['name']. "<br>"; echo "You are ". $_POST['age']. " years old."; ?> </body> </html>
Result
If the following input is provided to the test.php:
When "submit" button is clicked, it will open the greeting.php with the following result:
Example: $_POST example
The example below demonstrates the above discussed concept.
<html> <body> <form action="greeting.php" method="POST"> Name: <input type = "text" name = "name" /> Age: <input type = "text" name = "age" /> <input type = "submit" /> </form> </body> </html>
❮ PHP - Predefined Variables