POST Method In Action: Simple Form

Advertisement:

This Basic PHP tutorial illustrates the usage of POST method, with the help of a simple HTML form. It echo’s/print’s the user entered information on to the browser by passing it via POST method.

If you are sending sensitive informations like credit card number or your password, then make sure to use POST method. If you use GET method, it will reveal all the information in the address bar. With POST method, the parameters are not shown and hence not visible for people who might be stalking you!

Source Code
We have purposefully kept this example super simple, as this is a basic thing in PHP and complex things may confuse beginners.

postform.php

1
2
3
4
5
6
7
8
9
10
<html>
 <head><title>POST Method in Action</title></head>
 <body>
	<form action="post.php" method="post">
		Name <input type="text" name="user"><br />
		Company<input type="text" name="comp"><br />
		<input type="submit" value=" Submit Info">
	</form>
 </body>
</html>

This is a simple form which contains 2 input fields(user and comp) and a submit button. Observe the method used: its POST method.
We have given unique name to each input fields, so that the values are passed to the post.php

post.php

1
2
3
4
5
6
7
8
<?php
 
	$name       =	  $_POST['user'];
	$company  = 	  $_POST['comp'];
 
	echo "My name is $name <br /> And I'm the CEO of my company {$company}";
 
?>

This is the actual file where the action takes place. All the information is passed on to post.php via postform.php file. Using $_POST[] we receive the values and store it in the local variables. Using the values in these local variables we can do whatever we want to do: like print it on the browser(as we do in this tutorial), insert it into database etc.

Video Tutorial:



YouTube Link: http://www.youtube.com/watch?v=XrX02OSbPt4 [Watch the Video In Full Screen + HD]


Advantages of using POST method over GET method
The POST method does not have any restriction on data size to be sent.
The POST method can be used to send ASCII as well as binary data.
The data sent by POST method goes through HTTP header so security depends on HTTP protocol. By using Secure HTTP you can make sure that your information is secure.

3 thoughts on “POST Method In Action: Simple Form”

  1. This is a great tutorial! Form handling script seemed so difficult I was afraid it was beyond my capabilities. I do not plan on doing much programming in the future but I do need the ability to create basic post functions.

    This was the first PHP script that was easy enough for me to understand what is going on within the HTML and the PHP code.

    Thank you so much for sharing your abilities with us!

  2. @Carmen Savant, Thanks a lot for your feedback. I highly appreciate that.
    And make sure to come back whenever you need something related to programming.
    Again, thanks a lot for your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *