So, why, after almost a decade, am I revisiting this, and finally writing part 3? Part of my motivation is that I've seen a lot of bad uses of client-side scripting, that resulted in less-than-friendly, or at least not very intuitive or convenient forms and web pages. I've also seen a few simple things that can go a long way toward making web-based forms easier to work with, and more pleasant. Rather than explore JavaScript at length (as there are a number of tutorials that already do that), I've decided to focus on simple tips you can use to enhance your web-based forms, using client-side scripting.
Before continuing on, however, you may want to review parts 1 and 2 of this series, which were published in the May and June 1996 issues, respectively, of MUUG Lines, the newsletter of the Manitoba UNIX User Group. In part 1, we looked at how fill-out forms are coded in HTML. In part 2, we looked at the Common Gateway Interface (CGI), which lets you set up scripts to do server-side processing. If you're already comfortable with this, you should have no trouble with this third part. (You should, as well, already be familiar with JavaScript, as it would be used in HTML forms, since the following examples won't explain the code that is used in any detail.)
<script> function set_focus() { document.sample_form.input_field.focus(); } window.onload = set_focus; </script>The HTML code for the corresponding form would look something like this:
<form method="post" name="sample_form" action="/cgi-bin/sample.cgi"> <input type="text" size="30" name="input_field" value=""> <input type="submit" value="Go"> </form>Note the names given to the form and input field must correspond to those used in the script that refers to them. You are, of course, free to pick whatever suitable names you would like to use.
When the document containing the above script and form is loaded into a browser window, the keyboard focus will automatically be set to the named input field. The user can then start typing into that field right away, without having to click the mouse in that field first. However, it's not a big loss if scripting is disabled: the form still works as the user would expect, and only a bit of convenience is lost.
<input type="text" size="30" name="input_field" value="" onChange="this.form.submit()">But remember to include an actual submit button as well, in case JavaScript is not used by the client.
onChange
attribute, shown above, can be used to call up the
appropriate function to perform the error checking. Such a function
could issue an alert message in a pop-up window, or clean up the input
field, as appropriate.
In addition to checking fields as they are entered, you may also want to perform further checking on the entire form's content before allowing it to be submitted. That can be accomplished as follows:
<form method="post" name="sample_form" action="/cgi-bin/sample.cgi" onSubmit="check_form()">You would then include a function called
check_form()
within <script>
and </script>
tags. This function would do the appropriate checking,
then return a value of false
to prevent the submission from being done,
or a value of true
to continue with the submission. The onSubmit
attribute can alternatively be used on the input
tag for the submit
button, rather than on the form
tag.
Note that client-side error checking is merely a convenience for the user. It should never be used instead of server-side checking. Since the client-side checking may not even be performed, and since you may not have any control over what clients submit to the server anyway, you still need to do thorough checking of all input on the server side.
Since the possibilities are limited only by your imagination, and by the limitations of the scripting language used, there's a lot you can do. You could even have code that computes values to be filled in for particular fields, based on values in other input fields. Examples on the web abound, and some of them are quite sophisticated, such as various types of calculators that give users the results they need right from the browser - without having to actually submit the form to a server for processing!
label
tags
around the text associated with particular input fields (check boxes and
radio buttons, in particular), you make it easier to select these fields
by clicking, since users can now click anywhere within the label text.
For example:
<input type=radio name=sort_order value="A" id="ascend"><label for="ascend"> Ascending</label> <input type=radio name=sort_order value="D" id="descend"><label for="descend"> Descending</label> <input type=checkbox name=full value="yes" id="full"><label for="full"> Long format</label>Now, instead of having to click exactly in the radio button or check box, the user can click on the associated label text to select that particular input. Label tags can also be used in a similar way for text input fields, so that the keyboard focus goes to that input field when the label is clicked.
Note that this example is also in keeping with my philosophy of simply enhancing the form in a non-intrusive way, and that the form will still function correctly in older browsers that don't recognize the label tags.
This article is copyrighted by MUUG and the specific author(s). You are granted permission to duplicate it for non-commercial purposes only, provided it is not modified and includes this copyright notice as well as all author credits and attributions.
If you found this useful, you might also be interested in other MUUG tutorial articles. Or, why not find out more about MUUG? If you live in or near the Winnipeg area, why not check out one of our monthly meetings?