Updated April 11, 2023
Introduction to PHP $_POST
PHP comes out a function of $_POST that is a super global variable that is used to collect the data. After submitting an HTML Form some data is generated which is collected using a specific method POST. $_POST is also used for passing variables. It is basically passed to the HTTP POST method as the content type in the request. This is a super variable/automatic global variable whose scope is available throughout the script that means there is no need to declare a global variable to access this. This method _POST is available after the stable release of PHP 4 and above.
Syntax:
The Syntax for getting details using $_Post function is:
<?php
$_POST['name']
?>
"$_POST[…]" is the PHP array
'name'" is the URL variable name.
Screenshot :
$_POST Variable Working
The $_POST variable collects the data from the HTML form via the POST method. When a user fills the data in a PHP form and submits the data that is sent can be collected with the _POST Method in PHP. The Post Method transfers the information via the Headers. The Information is encoded using a scheme where the KEY/VALUE pairs are joined with an equal sign. All the special characters are removed and the data is encoded. Once we get the encoded data it is sent to a header called QUERY_STRING.
It doesn’t have the limit of data size to be sent in this QUERY_STRING. Both Binary, as well as ASCII data, can be sent of with the POST method. The data is then passed with some security protocol making the information secured and then sent to an associative array using the $_POST method.
The data sent by the POST Method is invisible to others and we cannot see any form of data when passed on. _POST is an array of variable passed to script via the HTTP Post Method.
The time spent by method POST is more than that of the other request that is why it has some what low performance, But it is widely used as the exposure for this is more as it supports many data types such as String, numeric, binary. And since the Values are not visible the result cannot be book marked.
The Post method is used for creating complex Form in HTML and to handle complex data are sent over the network.
So after the URL is Hit the URL will be something like that with the POST Method:
Localhost/pagename.php
Examples of PHP $_POST
Let us see some functioning of $_POST with some Example:
Example #1
Let’s start by creating an HTML form and see how the Request Generates.
Method Used Post:
HTML FORM:
This is HTML form.
<form method="post" action="<?php echo "hello PHP";?>">
Your Name: <input type="text" name="pname">
<input type="submit">
</form>
PHP POST Request Method:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['sname']);
if (empty($name)) {
echo "Empty Name";
} else {
echo $name;
}
}
?>
We are trying to create a FORM that takes Input as the Name, a request is submitted from the method POST with certain details and the data is collected by the Request Method.
Here the htmlspecialchars($Request[‘fname’]) takes the value of Input Field and checks whether any condition is given over there or not. If any condition is there that is checked or else the data is collected. So the Output for this will be something like:
Screenshot:
Let us check some more example:
Let’s create a Form that takes data from two field Name and Email of a user and let’s collect that using the POST Method. We will create a PHP File called Demo.php
Here is the Code:
<html>
<body>
Hey the name is <?php echo $_POST["name"]; ?><br>
With the email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
Create an HTML form with the Request with POST.
Code:
<html>
<body>
<form action="Demo.php" method="post">
The Name: <input type="text" name="Name"><br>
The E-mail: <input type="text" name="Email"><br>
<input type="submit">
</form>
</body>
</html>
Screenshot:
When we request or run this PHP code the request is generated and data is sent for processing.
Example #2
Let us check one more simple Example:
<form action="Demo2.php" method="post">
<p>Add<input type="text" name="Add" /></p>
<p>City<input type="text" name="City" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
Simple POST Request to get the Address and City
<?php
echo("Address: " . $_POST['Add'] . "<br />\\n");
echo("City " . $_POST[‘City'] . "<br />\\n");
?>
Screenshot:
We can use the isset function to check whether the value for this variable has been set or not. We can use conditional statements also with this method.
Code:
<?php
echo("Address: " . $_POST['Add'] . "<br />\\n");
echo("City " . $_POST['City'] . "<br />\\n");
?>
<form action="Demo2.php" method="post">
<p>Add<input type="text" name="Add" /></p>
<p>City<input type="text" name="City" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
Conclusion
From the above article, we saw the use of Function $_POST in PHP. From various example and classification we tried to understand how the $_POST function works in PHP and what are is use in the programming level.
We also saw the internal working and the advantages of having the type of data which we are defining for various programming purpose. Also, the syntax and examples helped us to understand much precisely the function.
Recommended Articles
This is a guide to PHP $_POST. Here we discuss the introduction to PHP $_POST, How PHP $_POST works in PHP, and Examples with code implementation?. You can also go through our other suggested articles to learn more –