Solved

verify & redirect based on radio selection

Posted on 2003-12-04
12
654 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now