Submit form conditionally - JavaScript
Author: SiteArticles.com
In the long-gone days of the early Internet, having a form on
the website was tantamount to dabbling with "cutting-edge"
technologies. Intricate Perl scripts and esoteric CGI scripts
were required to process those forms and people used to suffer
bouts of cold sweat whenever there manifested a need to use web
forms. To create a form-handling script used to be in the realms
of MCAs and computer engineers. No longer is it so. The Internet
these days is replete with all sorts of form handling scripts,
and wherever you choose to host your website, or for that matter
even a web page, you can easily deploy a form handling script
and start interacting with your visitors.
As the level of interaction goes complex, you require more
complicated scripts. One way is to write a single script
containing hundreds of lines of code. The action script (that
comes within ) encompasses
numerous if-then-else conditions, and even within these
conditions, there could be zillions of nested if-then-else
conditions. After a certain time it becomes a Herculean task to
maintain such a form handling script and unless you are an avid
documenter, you'll lose the track in no time.
The second way is, write smaller scripts and let the form call
them according to the user input. This can save you hundreds of
lines of coding, and even if it doesn't, it makes things a lot
easier. Suppose you have a form that, along with other things,
asks the visitor to which state she belongs. Then, when she
clicks the submit button, the action happens according the state
she selected. If you have a single script and if you want the
script to act according to the individual state, you might end
up writing a very large action script. On the other hand, if you
specifically write a script for, let us say, California; then
you have to write code only centered around California and you
can, for the time being, forget about other states.
Javascript lets you submit a single form, conditionally, to
different script. Here, we'll learn how to achieve this. First,
let us go through a simple form:
Choice 1 Choice 2 Choice 3
As you can see, this form displays three radio buttons. The
objective is, send the form to a script according to the radio
button selected. Since some Javascript action needs to take
place once the Submit button is clicked, we invoke
decide_action() function through the onSubmit attribute of the
tag. Although we include the action attribute, it is left
blank. The other form fields are the usual ones. Now let us dive
into the cryptic world of the actual script that steers the
submission.
function decide_action() { if(check_buttons()==true) {
if(document.frm1.ch[0].checked==true) {
document.frm1.action="one.php"; } else
if(document.frm1.ch[1].checked==true) {
document.frm1.action="two.php"; } else {
document.frm1.action="three.php"; } document.frm1.submit();
} }
function check_buttons() { var ok=false; for(i=0; i
More Articles From Javascript:
Random Articles:
;