How to Use the HTTP Protocol | 3 | WebReference

How to Use the HTTP Protocol | 3

How to Use the HTTP Protocol

Let's take a look at the code, line by line. First, we begin the form by using the <form> tag, and in this example we give the tag two attributes.

The action attribute determines the URL to which the submitted form will be sent. This may be to another page on the same server and described by a relative path, or to a remote domain, as in the code behind the form in Figure 3.1.

Next we find the attribute method, which determines whether we want the data to be submitted with a GET or a POST request.

Now suppose that we completed the form by entering the value Ballard into the surname field. On submitting the form by clicking the Send button, we are taken to http://www.sometargetdomain.com/somepage.htm, where the submitted data will be processed—perhaps adding the surname to a database, for example.

The variable surname (the name attribute given to the Your Surname input field) and its value (the data we entered in that field) will also have been sent to this destination page, encoded into the body of the POST request and invisible to users.

Now suppose that the first line of the form code reads as follows:

On using the form, we would still be taken to the same destination, and the same variable and its value would also be transmitted. This time, however, the form would construct and send a GET request containing the data from the form. Looking at the address bar of the browser, after successfully submitting the form, we would find that it now contains:

Here we can see how the parameter and its value have been appended to the URL. If the form had contained further input fields, the values entered in those fields would also have been appended to the URL as parameter=value pairs, with each pair separated by an & character. Here's an example in which we assume that the form has a further text input field called firstname:

Some characters, such as spaces and various punctuation marks, are not allowed to be transmitted in their original form. The HTML form encodes these characters into a form that can be transmitted correctly. An equivalent process decodes these values at the receiving page before processing them, thus making the encoding/decoding operation essentially invisible to the user. We can, however, see what this encoding looks like by making a GET request and examining the URL constructed in doing so.

Suppose that instead of the surname field in our form we have a fullname field that asks for the full name of the user and encodes that information into a GET request. Then, after submitting the form, we might see the following URL in the browser:

Here the space in the name has been replaced by the + character; the decoding process at the receiving end removes this character and replaces the space.

The XMLHTTPRequest object at the heart of all Ajax applications uses HTTP to make requests of the server and receive responses. The content of these HTTP requests are essentially identical to those generated when an HTML form is submitted.

Summary

This lesson covered some basics of server requests and responses using the HTTP protocol, the main communications protocol of the World Wide Web. In particular, we discussed how GET and POST requests are constructed, and how they are used in HTML forms. Additionally, we saw some examples of responses to these requests that we might receive from the server.

This chapter is excerpted from the book titled, "SAMS Teach Yourself Ajax in 10 Minutes," authored by Phil Ballard, Copyright 2006 by Sams Publishing. ISBN 0-672-32868-2, published April, 2006. Excerpt is from Chapter 3, pages 27-35, published by Sams Publishing. Reproduced by permission of Pearson Education, Inc. All rights reserved.

Created: March 27, 2003
Revised: May 22, 2006

URL: http://webreference.com/programming/protocol/1