Oj Obasi
Oj Obasi

I am also and will continue to be a student of web design and development. My tutorials may not be for you. If you feel that way, then search online for better tutorials.

PHP 5 Form $_POST method

Here we have an html form with two input fields, firstname and lastname.
We will be posting our form to the same php file, so we will use the super global variable( $_SERVER["PHP_SELF"]) to return the file name of our page.

In this example we will use the 'method' 'post' and we will be collecting the form data using the $_POST superglobal.

Your Html Form With Post as Method:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="200" border="1">
<tr>
<td><label for="firstname">First Name</label></td>
<td><input type="text" name="firstname" id="firstname"></td>
</tr>
<tr>
<td><label for="lastname">Last Name</label></td>
<td><input type="text" name="lastname" id="lastname"></td>
</tr>
<tr>
<td><input type="submit" value="Submit Form!"></td>
</tr>
</table>
</form>
Results of post form:<br>
Your first name is <?php echo $_POST["firstname"]; ?> <br>
And last name is <?php echo $_POST["lastname"]; ?> <br>

Your full name is <?php echo $_POST["firstname"]; ?> <?php echo $_POST["lastname"]; ?>

Lets say, you filled the form with the following information; firstname = Oj and last name = Obasi.
When the form is submitted, you will get the following results:
Your first name is Oj
And last name is Obasi
Your full name is Oj Obasi

Why use POST?

Well, most developers use the POST method for sending data for the following reasons:
When a form is submitted using the POST method, its data is invisible to others, all values are embedded within the body of the HTTP request and has no limits on the amount of information to send.

POST supports advanced functionality such as support for multi-part(enctype="multipart/form-data") binary input for uploading files to a server.

NOTE: When ever you think of form processing , always think of security concerns. In this tutorial , we didn't talk about validation which is a very important part of web form processing.