Solved

verify & redirect based on radio selection

Posted on 2003-12-04
12
655 Views
Last Modified: 2012-06-22
Here is my code:

<script language=JavaScript>
function checkForm(theForm)
{
if (theForm.results.value=="")
{
alert("Please make a choice.");
return (false);
}
return (true);
}
</script>


<script language = "JavaScript">
function deturl()
{
if (document.forms[0].option2[0].checked)
window.location.replace("blank.htm");
else
window.location.replace("main.asp");
}
</script>


<FORM name="MForm" METHOD="POST" onsubmit = "return checkForm(this); deturl()">


<input type=text name="results">


<INPUT TYPE="radio" NAME="option2" VALUE="value1" onClick='MForm.results.value="match";'>
<INPUT TYPE="radio" NAME="option2" VALUE="value2" onClick='MForm.results.value="nomatch";'>


<INPUT type=submit value ="Submit">

The validation works fine but it does not redirect.

Thanks,

Don
0
Comment
Question by:deross
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 9877791
You can't call anything after a return statement:

   onsubmit="return checkForm(this); deturl();"

will not work because the method stops at return.

You can try:

   onsubmit="var rc=checkForm(this); if (rc)deturl(); return rc;"
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 9877811
what exactly are you trying to do anyway?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9877831
1. what is the point of filling something in and then replace a page instead
2.


<FORM >
<INPUT TYPE="radio" NAME="option2" VALUE="value1" onClick="window.location.replace('blank.htm')">
<INPUT TYPE="radio" NAME="option2" VALUE="value2" onClick="window.location.replace('main.asp')">
</form>
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:deross
ID: 9877854
A user has two radio buttons to select from plus a submit button.

First, I want to make shure the user has selected one of the two radio buttons before submitting.

Second, depending on which of the two radio buttons the user selected, they will be directed to 'page1.htm' (radio button #1) or page2.htm' (radio button #2).

Don
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9877855
when the form is submittd the page that is the action property of the form is opened ie where the form data is sent to. The redirects must take place in the asp page that the data is being sent to
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9878043
yup...
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9878251
I am not sure that this is relevant, but if you use the location.replace, all of your form data will be lost. So, replace may not be the way to go.

I would suggest instead that you:

1) validate that a selection has been made
2) based on the selection, dynamically change the action attribute of the form

Fritz the Blank
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 500 total points
ID: 9878315
Something like this perhaps?

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function checkRadioControl(strFieldName,strMessage){
      var objFormField = document.forms[0].elements[strFieldName]
      intControlLength = objFormField.length
      bolSelected = false;
      for (i=0;i<intControlLength;i++){
            if(objFormField[i].checked){
                        bolSelected = true;
                        break;
            }
      }      
      if(! bolSelected){
            alert("Please select one option for " + strMessage + "!");
            return false;
      }else{
            return true;
      }
}

function checkForm(){
      if(checkRadioControl('option2','Value')){
            return true;
      }
      return false;
}
      
      
//-->
</SCRIPT>


</HEAD>
<BODY>
<FORM name="MForm" METHOD="POST" onsubmit = "return checkForm(this)">
<input type=text name="results">
<INPUT TYPE="radio" NAME="option2" VALUE="value1" onClick="document.forms[0].action =' blank.htm'">
<INPUT TYPE="radio" NAME="option2" VALUE="value2" onClick=document.forms[0].action =' main.asp'">
<INPUT type=submit value ="Submit">

</BODY>
</HTML>


Fritz the Blank
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9879688
fritz_the_blank, Where is the form data being sent to?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9882448
If  the first radial is selected, the form will post to blank.htm; if the second, to main.asp.

Fritz the Blank
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9888450
onClick="this.form.action =' blank.htm'">
would do it too...
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 9888818
>> onClick="this.form.action =' blank.htm'" <<

Ah, but that makes too much sense!

I'll remember to do it that way next time, Michel.

Fritz the Blank
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question