blog-header

Words with a View

Friday, February 17, 2006

Should I GET or should I POST ?

( title inspired by : Should I Stay or Should I Go - The Clash)

Usually in PHP/HTML if you want to submit a form to the server, you can basically use two methods: you can either use the GET method or the POST method.

form enctype="application/x-www-form-urlencoded" action="processRequest.php" method="GET" name="frm1"
input value="testMe" name="txtBox" type="text"
input value="send" name="submit" type="submit"
/form

Below is the resulting request that would be sent to the web server when the form was submitted using GET.
(it is appended to the URL).

GET /processRequest.php?txtBox=testMe&submit=send HTTP/1.1
Host: mysite.com
and you can access the variables in PHP using
if($_SERVER['REQUEST_METHOD']=='GET'){
$txtBox=$_GET['txtBox'];
$submit=$_GET['submit'];
//process request
}

form enctype="multipart/form-data" action="processRequest.php" method="post" name="frm2"
input value="testMe" name="txtBox" type="text"
input value="send" name="submit" type="submit"
/form

Below is the resulting request that would be sent to the web server when the form was submitted using POST.
(it is appended to the actual content portion along with the length of the content rather than appended to the URL).

POST /processRequest.php HTTP/1.1
Host: mysite.com
Content-length: 32
txtBox=testMe&submit=send
and you can access the variables in PHP using
if($_SERVER['REQUEST_METHOD']=='POST'){
$txtBox=$_POST['txtBox'];
$submit=$_POST['submit'];

//process request
}

So what's the difference?
Read On...

* if you specify the method as GET or you don't specify anything, the browser appends the data onto the URL.
http://mysite.com/products/getProductInfo.php?id=123
&type=hardware
* if you specify the method as POST the data is sent as standard input.
http://mysite.com/products/getProductInfo.php

You can use the GET method in the following scenarios:

# when you just want to get ( pun intended ) some data such as a query to list all products ( eg; a SELECT query).
# you used pagination and can use GET to get to the second page from a list of all pages.
1 2 3 4 5 6 7 8 9 10 Next 10
http://mysite.com/products/getProductInfo.php?id=123
&type=hardware&page=2

# to confirm registration for a subscription service such as newsletter, the service provider will mail you a confirmation mail back to you once you shows interests in thier service after entering say, your mailaddress.This URL where you have to confirm is a url appended with the variables.
http://www.myService.com/basePage.php?confirmationCode=mhtkhg
&confirmationEmail=fakemail%40rediffmail.com

See the variables does'nt even have to come from a form itself.

Pros:
# U can bookmark this and can also be used to paste this link in some other page or can use this specific information on social bookmarking sites such as digg.com or delicious.com for other users to jump directly into the specified page without any form submission.
# The variables can be changed directly from the location bar of the browser and has nothing to do with the form values.
# GET request is often cacheable.
Cons:
# Maximum URL length cannot be greater than 2,000 characters in that case you have to resort to POST.

You can use the POST method in the following scenarios:

# when you just want to update( changing values in a database, such as a DML statement), order a product( invovling a cash transaction).
# when sending mails or whenever your enctype is multipart/form-data for eg; in case of image or a file upload; ie, sending any non-ASCII characters to the server.

# when subscribing to a service such as newsletter, when first entering your eg: emailId or other unique values.
# when you don't want to let the user see what is submitted to the server.

Pros:
# actual form values can be greater than 2000
characters.
Cons:
# you can't cut and paste the information into a link.
# POST requests is only possible by using a form tag.


So have your say, Should I GET or should I POST???



0 Comments:

Post a Comment

<< Home