Handling forms

Objectives

Special note for HTML5

HTML5 has new form elements and some automatic validation. These notes cover pre-HTML5 forms since there is still a lot of variation in what browsers support as these notes are being written.

Preliminaries

It would be useful to have a page we go to after the form is validated. Getting to that page would provide a clear indication that the validation code let the data through, while not getting to the page would make it clear that something didn't validate. For the sake of today's demonstrations, we will name this file receiver.html. The code is as follows:

<html> <head> <title></title> </head> <body> <h2>I received your message</h2> </body> </html>

Note on more modern technique

The examples on this page show a number of ways of accessing form field values. In almost all cases, it is now preferable to give fields an ID value and then get a reference to them using document.getElementById(). Both the older and newer styles are included in the examples on this page.

Requiring a form field

Comparing form fields

Using an image to submit a form

Drop down list navigation

Modifying a drop down list