Updated April 14, 2023
Introduction to PHP POST Method
PHP post methods are one of the ways by which the browser client is able to send required information to the webserver. Basically, when we have to log in to a website or interacting with our mails, it means we are dealing with a form. Forms are used to fetch and submit the information taken from the user to the webserver. They are basically HTML tags that have graphical user interface items like the input box, radio buttons, checkboxes, etc,.
A form is defined using the <form> tags and GUI items described using input form elements and come in use when dealing with the development of adjustable and dynamic applications that allow user input. Forms can also be used to alter the existing data in the database information.
Syntax:
<?php
$_POST['var'];
?>
Where,
- var is the variable name of the URL
- $_POST being the PHP array
How Does PHP POST Method work?
This method submits the value via HTTP headers and since the above variable has a global scope, it can be obtained from anywhere in the program. This method is best used when the form post values are not to be shown in the URL.
Some of its properties are:
- It does not impose any restrictions on the size of the data to be sent.
- The data transfer occurs through HTTP header and hence the security also depends on the same. Hence by making use of the HTTPS method we can make sure it is secure.
- By using the POST method we can access both ASCII and binary data.
- The $_POST is the array which accesses all the information sent by this method.
Examples to Implement PHP POST Method
Below are the examples mentioned:
Example #1
Code:
<html>
<body>
<form action="Test.php" method="post">
First Name: <input type="text" name="name"><br>
Last Name: <input type="text" name="name"><br>
Mail ID: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
Output:
Explanation: In this example, we shall see a simple HTML form submission where the form consists of 3 attributes: First Name, Last Name, and Mail ID. When the user fills all the information and then clicks on the submit button, the data entered is sent using the HTTP POST method.
Example #2
Code:
<?php
if (!empty($_POST))
{
// Defining an array for the list of post values for all the
// different forms on the page
$pNameArray = array('Form1', 'Form2', 'Form3');
// To search all post identifiers that fall within $_POST
$postIdArr = array();
foreach ($pNameArray as $pName)
{
if (array_key_exists($pName, $_POST))
{
$postIdArr[] = $pName;
}
}
// There should be only one post identifier at a time and hence
// only an individual form should be used for submitting at a time
// A warning statement can be used instead of the die statement as used here
if (count($postIdArr) != 1)
{
count($postIdArr) < 1 or
// validation of count
die("\Validating count here: " .
implode(" ", $postIdArr));
// The above condition is not satisfied hence there is only one post identifier
die("\Presence of unknown post");
}
// Using switch statement to execute the codes for respective forms
switch ($postIdArr[0])
{
case 'Form1':
echo "Execute actual code for Form1.";
break;
case 'Modifyform':
echo "Execute actual code for Form2.";
break;
case 'Deleteform':
echo "Execute actual code for Form3.";
break;
}
}
else // $_POST is empty.
{
// displaying that POST is not executed
echo "POST is empty";
}
?>
Output:
Explanation: In this example, we are seeing how the POST method works for multiple forms. Hence we are using a switch statements to process different forms and all the forms should be distinct for it to process without any errors. If we need to add more forms, it just needs an entry to be made in the array and the switch statement.
Example #3
Code:
<?php
if( POST["fullname"] ||
POST["weight"] || POST["height"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['fullname'] )) {
die ("The name given is invalid and should be alpha");
}
echo "Welcome ". $_POST['fullname']. "<br />";
echo "Welcome ". $_POST['height']. "<br />";
echo "You are ". $_POST['weight']. "weight in kgs";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Full Name: <input type = "text" name = "fullname" />
Height: <input type = "text" name = "height" />
Weight: <input type = "text" name = "weight" />
<input type = "submit" />
</form>
</body>
</html>
Output:
Explanation: The query strings in this example are sent to the body of the HTTP message of the POST method request. Here we are specifying 3 attributes namely full name, height, and weight.
Advantages of POST method over GET method in PHP
- The POST method is more secure as compared to GET because the information entered by the user is not shown in the URL query string or in the logs of the server.
- The range of the data which can be passed is more in POST than in the GET method since it can send both text data and binary data such as uploading a file using the POST method. It supports many such advanced functionalities.
- It supports various different data types like numeric, string, binary types.
Conclusion
As shown above, basically PHP Post method is used to collect form data. Hence using the HTTP Post method to send information over web servers. Since this information is never visible in the address bar it is often used to send over sensitive data over the servers. They do also have a few disadvantages in terms of performance, POST method requests are not cached and hence they are not retained in browser history.
Recommended Articles
This is a guide to the PHP POST Method. Here we discuss an introduction, syntax, and working of the POST Method in PHP along with different examples and code implementation. You can also go through our other related articles to learn more –