Attributes of the <form> Element

The <form> element has a whole host of attributes, but you can get by using only two of them, action and method. Other attributes, such as id, class, dir, lang, language, name, style, and title are universal to most or all HTML tags, and shouldn't need further explanation. The more obscure attributes accept-char and enctype, which specify the character sets and the mime-type of the form data are outside the scope of this overview, although you'll learn more about enctype in Chapter 7. There is also a target attribute that, like its namesake in the <a> element, enables you to specify a frame or window in which to display the Web page that is sent back in response to the form's submission.

The action Attribute

The action attribute tells the server which page to go to once the user has clicked a submit button on the form. It doesn't matter whether this page is pure HTML, PHP, or uses any server-side technology, as long as the page exists on the Web server. It can be used as follows to link to an HTML page:

<form action="myprogram.php">
...
</form>

However, when you supply a PHP page as part of the action attribute, what you're actually doing is sending the information entered into this form to the PHP script engine for processing, thereby allowing your PHP application on the server to work with user-supplied data. The action attribute just tells the server which page to go to next—if you saved the earlier page as myprogram.html instead of myprogram.php, the page wouldn't be sent to the PHP script engine and nothing at all would be displayed unless PHP has been configured to parse .html files. Later you'll see what the PHP script engine does when it receives the form.

The method Attribute

The method attribute controls the way that information is sent to the server. As mentioned previously, it can do this in one of the two ways, using either the GET or the POST methods (they are case-insensitive, but the convention is uppercase). The GET method is the default; you use these methods as shown here:

<form action="myprogram.php" method="GET">

or

<form action="myprogram.php" method="POST">

There are actually more than two values you can supply to the method attribute: HEAD, PUT, LINK, UNLINK, OPTIONS, DELETE, TRACE, and CONNECT. However, these options are not commonly used, and you rarely need to use them, so there's no further discussion of them here. You will, however, want to take a closer look at GET and POST.

The GET Value

The GET value of the method attribute tells the browser to append the values the user placed on the form to the URL. Like the query string attached to a URL in a link, the browser adds a question mark to the end of the URL to denote where the URL finishes and the form information starts. The information entered into the form is then transmitted as name/value pairs. If the GET method is specified in the method attribute of the <form> element, the browser automatically appends the information to the URL when it sends the page request to the Web server.

And just like a query string in a link, you can add more than one name/value pair to a URL if you separate each pair with an ampersand (&). With two name/value pairs, the end of the URL might look like this after being submitted:

To conform with the XHTML specification, you would want to replace the ampersand with &amp;, which is the appropriate XML entity for an ampersand, so your URL and query string would look like this:

As mentioned earlier, the name/value pairs are very similar to variables. In fact, once they have been passed to the Web server for processing, PHP makes them available as variables. So, if you submitted your form to the Web server, and moved to another page, the name/value pairs would be available in the PHP script as variables (as part of the $_GET array).

Occasionally, you might want to pass spaces in the values that make up the query string. For instance, if you have a form that includes a <textarea> element and someone had typed in the line, "I would like to see a dynamic menu in operation," there are several spaces that need to be represented. In this case the addition operator would replace the spaces.

Some of you are wondering, "But what happens if you want to put an addition operator in the <textarea>? How is that represented within a query string?" The character or operator in question has to be replaced by a code, which signifies the particular character. This is known as URL encoding.