When we are using html form, we can send data to another page using two different methods
- POST method
- GET method
Both serves same purpose, but the internal mechanism is bit different. When using GET method key value pairs are passing through browser navigation bar. But for POST method a visitor cannot see the data through browse, basically it sending via HTTP headers.
GET Method
Spaces are removed and replaced with the + character or any other non alphanumeric characters are replaced with a hexadecimal values. Values are URL encoded for better handling.
A sample GET method request will look like this
http://domian.com/index.php?categroy=outdoor&product=barbiedoll
The encoded string should pass after a question mark, otherwise we cannot parse the GET data.
- GET method produces a long string that we can see on the browser navigation bar.
- We can send upto 1024 characters using GET method.
See the below examples to get an understanding how GET method sending data. If you don’t specify a method in the form, default is GET.
<form action="formget.php"> <input type="text" name="fistname" > <input type="text" name="lastname" > <input type="text" name="email" > <input type="text" name="mobile" > <button name="button" value="OK" type ="button">Submit</button> </form>
This will create a encoded URL like
http://domain.com/formget.php?fistname=John&lastname=Doe&[email protected]&mobile=909909
So your form will be sending all data to formget.php, which is a server side script. Next, we will see how to retrieve these GET values in a server side script, like PHP.
formget.php
<?php echo "Firstname: ". $_GET['firstname']. "<br />"; echo "Lastname: ". $_GET["lastname"]. "<br />"; echo "Email: ". $_GET["email"]. "<br />"; echo "Mobile: ". $_GET["mobile"];
When using GET method all the form values are exposed to public, so never use the GET method for sensitive data like passwords, or credit card information etc.
POST method
Using POST method is more safer than GET method and more data can transfer using POST method, there is no restriction for sending data. POST method transfers the data via HTTP headers.
See below an example of POST method form, you have to explicitly add the form method in order to do POST form
<form action="formpost.php" method="post"> <input type="text" name="fistname" > <input type="text" name="lastname" > <input type="text" name="email" > <input type="text" name="mobile" > <button name="button" value="OK" type ="button">Submit</button> </form>
So in formpost.php, you can get the detials as follows
echo "Firstname: ". $_POST['firstname']. "<br />"; echo "Lastname: ". $_POST["lastname"]. "<br />"; echo "Email: ". $_POST["email"]. "<br />"; echo "Mobile: ". $_POST["mobile"];
Conclusion
Both methods are using for transferring data from form to server side scripts. Do not use GET method while you dealing with sensitive data like passwords, credit card information.
You may be interested in this simple login script script, which uses these methods in a application
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply