Database Reference
In-Depth Information
A form begins and ends with <form> and </form> tags. Place other HTML constructs
between those tags, including elements that become input fields in the page that the
browser displays. The <form> tag that begins a form should include two attributes,
action and method . The action attribute tells the browser what to do with the form
when the user submits it. This is the URL of the script to invoke to process the form's
contents. The method attribute indicates to the browser what kind of HTTP request to
use to submit the form. The value is either get or post . Recipe 20.5 discusses the dif‐
ference between these two request methods; for now, we'll always use post .
Most of the form-based web scripts shown in this chapter share some common behav‐
iors:
• When first invoked, the script generates a form and sends it to the user to be filled
in.
• The action attribute of the form points back to the same script. When the user
completes the form and submits it, the web server invokes the script again to process
the form's contents.
• The script checks its execution environment to see what input parameters are
present. For the initial invocation, the environment contains none of the parameters
named in the form. This enables the script to determine whether it's being invoked
by a user for the first time or whether it should process a submitted form.
This approach isn't the only one you can adopt. One alternative is to place a form in a
static HTML page and have it point to the script that processes the form. Another is to
have one script generate the form and a second script process it.
If a form-creating script wants to have itself invoked again when the user submits the
form, it should determine its own pathname within the web server document tree and
use that value for the action attribute of the opening <form> tag. For example, if a script
is installed as /cgi-bin/myscript in your web tree, you could write the <form> tag like
this:
<form action= "/cgi-bin/myscript" method= "post" >
Each of our language APIs provides a way for a script to obtain its own pathname, which
enables you to avoid hardwiring a script's pathname into it and gives you greater latitude
where to install it.
Perl
In Perl scripts, the CGI.pm module provides three useful methods for creating <form>
elements and constructing the action attribute. start_form() and end_form() gen‐
erate the opening and closing form tags, and url() returns the script's own pathname.
Using these methods, scripts generate a form like this:
Search WWH ::




Custom Search