04.06.2013 Views

Head First HTML with CSS

Head First HTML with CSS

Head First HTML with CSS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Watching GET in action<br />

There’s no better way to understand GET than to see it in action.<br />

Open up your “form.html” file and make the following small change:<br />

there are no<br />

Dumb Questions<br />

Q: Why is it called “GET” if we’re<br />

sending something to the server?<br />

A: Good question. What’s the main<br />

job of a browser? To get Web pages from a<br />

server. And, when you are using GET, the<br />

browser is just going about getting a Web<br />

page in the normal way it always does,<br />

except that, in the case of a form, it has<br />

appended some more data to the end of the<br />

URL. Other than that, the browser just acts<br />

like it’s a normal request.<br />

With POST, on the other hand, the browser<br />

actually creates a little data package and<br />

sends it to the server.<br />

Q: So why would I use POST over<br />

GET, or vice versa?<br />

A: There’s a couple of big differences<br />

that really matter. If you want users to be<br />

able to bookmark pages that are the result<br />

of submitting a form, then you have to use<br />

GET, because there is no way to bookmark<br />

a page that has been returned as a result of<br />

a POST. When would you want to do that?<br />

Say you have a Web application that returns<br />

a list of search results; you might want users<br />

to be able to bookmark those results so they<br />

can see them again <strong>with</strong>out having to fill out<br />

a form.<br />

xhtml forms<br />

<br />

Save and reload the page; then fill out the form and submit it. You<br />

should see something like this:<br />

You’ll see this URL<br />

in your browser.<br />

Just change the method<br />

from “POST” to “GET”.<br />

http://www.starbuzzcoffee.com/processorder.php?beans=Kenya&beantype=gro<br />

und&extras%5B%5D=catalog&name=Buckaroo+Banzai&address=Banzai+Instit<br />

ute&city=Los+Angeles&state=CA&zip=90050&comments=Great+coffee<br />

Now you can see every<br />

form element name<br />

and their values right<br />

here in the URL.<br />

Notice that the browser encodes<br />

various characters, like spaces. The<br />

Web application will automatically<br />

decode these when it receives them.<br />

On the other hand, if you have a Web<br />

application that processes orders, then you<br />

wouldn’t want users to be able to bookmark<br />

the page. (Otherwise, every time they<br />

returned to the bookmark, the order would<br />

be resubmitted.)<br />

A situation when you’d never want to use<br />

a GET is when the data in your form is<br />

private, like a credit card or a password.<br />

Because the URL is in plain view, the private<br />

information is easily found by others if they<br />

look through your browser history or if the<br />

GET somehow gets bookmarked.<br />

Finally, if you use a , you should<br />

use POST, because you’re probably sending<br />

a lot of data. GET requests have a limit of<br />

256 characters; POST has no limit on the<br />

size of the data package you send.<br />

you are here 621

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!