/    Sign up×
Community /Pin to ProfileBookmark

Redirecting browser according to radio button chosen

Hi guys/gals,

I have two radio buttons in my form. when a user clicks the submit button i want the browser to take them to either [url]www.abc.com[/url] or [url]www.xyz.com[/url] according to which radio button they selected. However the following code does not seem to do the trick. The functions are called correctly as the alert messages are displayed correctly when i click the submit button. However it does not redirect the browser at all. Any help would be appreciated. the code is as follows:

[code]

<html><head><script language=”JavaScript”>
function checkRadio(){
var url;
if (document.checkform.radios[0].checked)
{
alert(“abc”);
window.location.href= “http://www.abc.com”;

}else if(document.checkform.radios[1].checked){
alert(“xyz”);
window.location.href= “http://www.xyz.com”;

}

}
</script>
</head>

<body>
<form name=”checkform” action=”” onsubmit=”checkRadio()”>
<p>
<input name=”radios” type=”radio” value=”radiobutton1″ />
<strong>I want to go to abc.com </strong></p>
<p>
<input name=”radios” type=”radio” value=”radiobutton2″ />
<strong>I want to go to xyz.com </strong></p>
<p><strong>
<input name=”Enter” type=”submit” id=”Enter” value=”Enter” />
</strong></p>
<p><strong>
<input type=”checkbox” name=”checkbox” value=”checkbox” />
Remember my choice
</strong></p>
</form>

</body>
</html>

[/code]

Thanks in advance ?
Also if i wanted to be able to remember the user’s choice so next time it automatically redirects him/her..whats the quickest way to do that..

cheers

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterSep 22.2005 — &lt;form name="checkform" action="" onsubmit="[color=royalblue]return[/color] checkRadio()"&gt;

<i>
</i>&lt;script [color=royalblue]type="text/javascript"[/color]&gt;
function checkRadio(){
var url;
if (document.checkform.radios[0].checked)
{
alert("ifa");

}else if(document.checkform.radios[1].checked){
alert("direct");
window.location.href= "http://www.hotmail.com";

}
[color=royalblue] return false;[/color]
}
&lt;/script&gt;


The LANGUAGE attribute is depreciated, get used to using the TYPE attribute from now on. Make friends with [url=http://validator.w3.org/]the validator.[/url]

[b]edit:[/b]

Also if i wanted to be able to remember the user's choice so next time it automatically redirects him/her..whats the quickest way to do that..
[/quote]


Look into cookies, I personally hate working with them, so maybe someone else will enter and give you some code.
Copy linkTweet thisAlerts:
@chelnov63authorSep 22.2005 — Thanks for that..but it makes no difference. It still does not redirect when i use <script type="text/javascript">
Copy linkTweet thisAlerts:
@UltimaterSep 22.2005 — Did you try the code with my other two changes included?
Copy linkTweet thisAlerts:
@chelnov63authorSep 22.2005 — Oh sorry never notcied the <form name="checkform" action="" onsubmit="return checkRadio()">

Thanks for your help it works great ? ..just wondering though why do we need to use onsubmit="return checkRadio()" instead of just onsubmit="checkRadio()" , since all the redirection is done in the function. So why do we have to return a value?

cheers
Copy linkTweet thisAlerts:
@UltimaterSep 22.2005 — Because [color=royalblue]alert("ifa");[/color] isn't redirecting to anywhere in the example and it's good coding to return specific values.
Copy linkTweet thisAlerts:
@chelnov63authorSep 23.2005 — thanks for all your help ? cheers
×

Success!

Help @chelnov63 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...