The POST method is simply a way of passing data to a server. In this tutorial we will help you to get familiar with retrieving data (which was sent via HTML forms) using PHP.
When data is sent using an HTML form to the server, PHP will intercept this data and create an array which holds all the data.
When the POST method is used to send the data, PHP will store the data in the superglobal $_POST associative array. If we want to retrieve the data, we would have to retrieve it from this array as described in our PHP array tutorial.
Retrieving data that was sent using the POST method is straightforward.
$variable = $_POST['parameter'];
That's all there is to retrieving data passed to a script using the post method.
Suppose this form was sent:
<html> <body> <form action="index.php" method="post"> Name: <input type="text" name="name" /> School: <input type="text" name="school" /> <input type="submit" /> </form> </body> </html>
The URL it would go to would be:
The data itself would be wrapped inside the raw HTTP request that was sent to the server instead of the URL as we saw in our PHP $_GET Tutorial. This makes this method more suitable for sending sensitive information such as passwords so that the password would not be visible in plain sight.
If we had a script at the location referenced in the code above, we would be able to retrieve the values using:
<?php $name = $_POST['name']; $school = $_POST['school']; ?>
Once we had the variables we would be able to work on them as we desired.