JavaScript : Submit form conditionally

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:
<form method=”post” name=”frm1″ onSubmit=”javascript: decide_action();” action=”">
<input type=”radio” name=”ch” value=”one” /> Choice 1<br />
<input type=”radio” name=”ch” value=”two” /> Choice 2<br />
<input type=”radio” name=”ch” value=”three” /> Choice 3<br />
<input type=”submit” name=”s1″ value=”Submit” />
</form>
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 <form> 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.
<script language=”javascript”>
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<3; i++){
if(document.frm1.ch[i].checked==true){
ok=true;
}
}
if(ok==false){
alert(“Select at least one option.”);
}
return ok;
}
</script>