Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

verify & redirect based on radio selection

Posted on 2003-12-04
12
Medium Priority
?
662 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

618 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