Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP $_POST variable


May 11, 2021 PHP


Table of contents


PHP $_POST variable


In PHP, a predefined $_POST variable is used to collect values from a form that is "post" and $_POST is often used to pass variables.


$_POST variable

The predefined $_POST variable is used to collect values from forms that are "post" in the method.

The information sent from a form with a POST method is not visible to anyone (it does not appear in the browser's address bar) and there is no limit to the amount of information that can be sent.

Note: By default, however, the POST method sends a maximum of 8 MB of information (which can be changed by setting the .ini in the post_max_size php file).

Instance

 <form action="welcome.php" method="post">
 Name: <input type="text" name="fname">
 Age: <input type="text" name="age">
 <input type="submit">
 </form> 

When the user clicks the "Submit" button, the URL looks like this:

//www.w3cschool.cn/welcome.php

The welcome .php file can now collect form data from the $_POST variable (note that the name of the form field automatically becomes the key in the $_POST array):

Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.


When do I use method"post"?

Information sent from forms with post methods is not visible to anyone and there is no limit to the amount of information that can be sent.

However, because the variable does not appear in the URL, the page cannot be bookmarked.


PHP $_REQUEST variable

The predefined $_REQUEST variable contains $_GET, $_POST, and $_COOKIE content.

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

Instance

Welcome <?php echo $_REQUEST["fname"]; ?>!<br>
You are <?php echo $_REQUEST["age"]; ?> years old.