Many Actions With One HTML Form Submission
By
William
Bontrager
There are three ways to make one
form "submit" button click do more than one action:
-
Using the JavaScript onSubmit command
to run one or more JavaScript functions before sending the form information
to the regular form handling program on the server.
-
Using JavaScript to select one from
among several form handling programs to submit the form information to.
-
Sending the form information to a
form handling program on the server that then posts the form information
to other form handling programs.
The above can be combined.
The first two have been covered in earlier articles and tips. I'll mention
them briefly, with references.
The last is possible with a brand new Master Series CGI program.
How CGI Works
CGI requires three steps:
-
The browser sends something to a
program on the server.
-
The program on the server does something,
presumably something useful.
-
The program on the server sends something
back to the browser.
That's the CGI cycle. The program
on the server is called a CGI program because it accepts something from the
browser and sends something back to the browser, all according to the CGI
protocol.
The CGI protocol is too deep to wade into with this article, but it boils
down to the above three steps.
For further light reading on the subject, see the "How CGI Works" article
at
http://willmaster.com/a/24h/pl.pl?art243a.
Usually, the first step of the CGI cycle is accomplished through the use
of a form. Your visitor fills in one or more blanks and clicks a submit button.
Once the submit button is clicked, the form handling program (which is a
CGI program) does something with the information it received from the form
and then sends something back to the browser.
What's sent back to the browser is commonly a "thank you" page of some kind,
a receipt page, a download page, another form, or just an information page.
But something must be sent back to the browser or else the browser just sits
there waiting for an answer from the CGI program until the browser times
out.
If you want to acquire a basic understanding of how forms work and how to
use them, see the "HTML Form Tutorial" article at
http://willmaster.com/a/24h/pl.pl?art243b
Working Around the One-URL Action Restriction
In the past, form information was submitted to one form handling CGI program
on the server. The program URL was specified in the action="___" attribute
of the FORM tag. That was all that could be done.
It was all that was required for most web sites.
Some web sites, however, needed more.
"More" is what this article is about. More can be done while remaining true
to the CGI protocol and the one-URL form action restriction.
Using the JavaScript onSubmit Command
With JavaSCript, functions can be run with the onSubmit command. Form fields
can be checked for errors and messages could be sent to the user in the form
of alert boxes or popup windows, as examples.
Either or both the onSubmit command in the FORM tag and the onClick command
in the submit button input tag can be used for this purpose.
The JavaScript function is run before the form information is submitted to
the form handling CGI program. The function can return a value of false to
abort the submission, or a value of true to allow the submission to occur.
See the
"Multiple onClick Form Submit Attributes" tip for a method
of running multiple JavaScript functions whenever a submit button is clicked.
Using JavaScript to Select a Form Handling Program
Once in a while, a method of submitting the form information to an alternate
form handling CGI program is desired.
JavaScript can be used to change the action="_______" URL when certain form
fields contain certain information if "pay by check" is selected instead
of "pay by credit card," for instance.
The
"Changing Form Action URLs On-The-Fly" article contains
detailed instructions for accomplishing that.
The JavaScript onSubmit command can be used to run other JavaScript functions,
in conjunction with this method of selecting an alternate form handling program.
Multiple Form Handling Programs with One Submission
Master Form Action+ makes it possible to have form information submitted
to more than one form handling program. The Master Form Action+ product
description page is at
http://willmaster.com/a/24h/pl.pl?art243e
Master Form Action+ works by accepting the form information submitted to
it, then sending that information to other form CGI handling programs, in
sequence, according to what you've specified in the Master Form Action+ control
panel.
For example, let's suppose you have a bid request form and that you want
the form submission to go to three different form handling CGI programs.
This is only an example scenario. Master Form Action+ can actually do more
and in other ways. For more Master Form Action+ product information, see
http://willmaster.com/a/24h/pl.pl?art243e
In the stated example, the form's information is submitted to the Master
Form Action+ CGI program, which then:
-
The first form handling CGI program
is a follow-up autoresponder to send a note the next day saying that you're
diligently working on the bid. This autoresponder might be on your server
or it might be at a service you pay to send your messages.
You've set up the Master Form Action+ control panel so it sends the bid
requestor's email address and name to the autoresponder. You've also set
it up so that should this submission fail then a certain form field name
will contain a certain value, name "failed" with an "ouch!" value, for instance.
Once Master Form Action+ has submitted the email address and name to your
autoresponder program and received an answer back (it's CGI, so it waits
for an answer), it does the next step in the sequence.
-
On your bid request form you have
a checkbox the bid requestor can check to be added to your mailing list.
So you've set up the Master Form Action+ control panel to send the person's
email address to your list management CGI program if, and only if, the checkbox
is checked.
Like the autoresponder, the list management program can be on your server
or it can be at a list hosting service. Any URL on the Internet is good enough
for Master Form Action+.
-
The last form handling CGI program
in the sequence is your bid request information handling program. You've
set up Master Form Action+ so it sends all form information to the bid handling
program, including the autoresponder "failed" name and value if it's present.
In the Master Form Action+ control panel, you've specified that the page
to send back to the browser is the "thank you" page your bid request handling
program generates.
The last form handling CGI program
in the sequence is the only program that can send content back to the browser,
because sending content to the browser is the last step in the CGI cycle.
The first step in the CGI cycle is the form's initial information submission
to Master Form Action+. The second step is the entire sequence of form handling
CGI programs the information is submitted to. And the final step is sending
something back to the browser.
The content returned by the previous CGI programs in the sequence is dealt
with according to what you specify on the control panel, or ignored if you
don't specify anything.
The options are:
-
Specifying a field name and value
in case the submission is successful.
-
Specifying a field name and value
in case the submission is not successful.
-
Storing the code returned by the
server (200 for success, 404 for URL not found, etc.) in a field name you
specify.
-
Storing the content returned by the
form handling CGI program in a field name you specify.
Master Form Action+ makes it possible
to accomplish a lot of action with a single click of a submit button.
Will Bontrager
Copyright 2004 Bontrager Connection, LLC
About the Author: