/    Sign up×
Community /Pin to ProfileBookmark

Different Link depending on Checkbox(es)

I need to send people to a different URL depending on which checkboxes are selected. Would someone be kind enough to write me a code snippet?

EXAMPLE:
page with 3 checkboxes + “submit” button.
– box 1 checked = [url]http://www.yahoo.com[/url]
– box 2 checked = [url]http://www.altavista.com[/url]
– box 3 checked = [url]http://www.netscape.com[/url]
– box 1&2 checked = [url]http://www.msn.com[/url]
– box 1&3 checked = [url]http://www.google.com[/url]
– box 2&3 checked = [url]http://www.aol.com[/url]

If someone could help me code this I would be VERY grateful.

:Stacey (Javascript newbie)

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 21.2004 — Try this:

[code=php]
<html>
<head>
<script>
url = new Array();
url[0] = new Array();
url[0][0] = 'http://www.yahoo.com';
url[0][1] = 'http://www.google.com';
url[1] = new Array();
url[1][0] = 'http://www.altavista.com';
url[1][1] = 'http://www.msn.com';
url[2] = new Array();
url[2][0] = 'http://www.netscape.com';
url[2][1] = 'http://www.google.com';
function newURL(){
d = document.forms[0];p2 =-1;p1 =-1;
for(i=0;i<d.ch.length;i++){
if (d.ch[i].checked){
if(p2 == 1){
if(i == d.ch.length-1){alert('too many boxes checked!');return false}
else {p1 = 0;break}
}
else {p1 = i;p2++;}}}
window.open(url[p1][p2],'_self');
}
</script>
</head>

<body>
<form>
<input name="ch" type="checkbox"><br>
<input name="ch" type="checkbox"><br>
<input name="ch" type="checkbox"><br><br>
<input type="button" onclick="return newURL()" value="GO URL">
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@idesign123authorFeb 22.2004 — Thanks for the great code (I didn't even think about having all 3 checked!).

Unfortunately I can't get it to work.

I have two tests online...

- http://207.44.169.26/test.html

- http://207.44.169.26/test.php

  • * I wasn't sure whether the file needed to end in .html or .php so I made one of each. I set permissions on the php file to 755.


  • On these I just cut/paste the code, but nothing is happening when I click the submit button. Am I missing a step?

    Thanks tons,

    :Stacey
    Copy linkTweet thisAlerts:
    @Paul_JrFeb 22.2004 — If you're using some kind of editor or something, it seemed to have inserted some weird characters where there was a space... the script works exactly "as is". Are you using any sort of editor or somethin'?
    Copy linkTweet thisAlerts:
    @idesign123authorFeb 23.2004 — Oops. you're right (GoLive must have munged the code).

    I just copied the code again, bypassing GoLive and instead just using a text editor. Still no luck though. ?

    New tests are at...

    http://207.44.169.26/test2.html

    http://207.44.169.26/test2.php

  • * the code looks correct now, but nothing happens when you press "submit".


  • Ideas? I feel bad asking again, but really need help with this one.

    :Stacey
    Copy linkTweet thisAlerts:
    @Paul_JrFeb 23.2004 — Works fine for me.

    But FB's JS Console says "url[p1]" has no properties. I don't think the page is supposed to change unless a checkbox is checked anyway.
    Copy linkTweet thisAlerts:
    @KorFeb 23.2004 — OK. To avoid the all unchecked situation, I add a condition:

    [code=php]
    <html>
    <head>
    <script>
    url = new Array();
    url[0] = new Array();
    url[0][0] = 'http://www.yahoo.com';
    url[0][1] = 'http://www.google.com';

    url[1] = new Array();
    url[1][0] = 'http://www.altavista.com';
    url[1][1] = 'http://www.msn.com';
    url[2] = new Array();
    url[2][0] = 'http://www.netscape.com';

    url[2][1] = 'http://www.google.com';
    function newURL(){
    d = document.forms[0];p2 =-1;p1 =-1;
    for(i=0;i<d.ch.length;i++){
    if (d.ch[i].checked){
    if(p2 == 1){
    if(i == d.ch.length-1){alert('too many boxes checked!');return false}
    else {p1 = 0;break}
    }
    else {p1 = i;p2++;}}}
    if(p2 == -1){alert('You have not checked any box!'); return false}
    window.open(url[p1][p2],'_self');
    }
    </script>
    </head>

    <body>
    <form>
    <input name="ch" type="checkbox"><br>
    <input name="ch" type="checkbox"><br>
    <input name="ch" type="checkbox"><br><br>
    <input type="button" onclick="return newURL()" value="GO URL">
    </form>
    </body>
    </html>
    [/code]
    Copy linkTweet thisAlerts:
    @idesign123authorFeb 23.2004 — Hey that works great (on my PC)!

    Grateful thanks. ?

    ONE MORE QUESTION:

    Does anyone know why this script does not work on my Mac (tried in both Internet Explorer and Safari - you press submit and it just sits there). About 20% of our visitors are on Mac so I would love to make it work on Mac too (if that's possible).

    Thanks again,

    :Stacey
    Copy linkTweet thisAlerts:
    @scmoulJul 03.2006 — Can someone help me add another checkbox, such that there are now four and nine different url's and not just six???
    Copy linkTweet thisAlerts:
    @scmoulJul 03.2006 — this is the code i have so far. the problem is that when i click 3 or 4 it gets confused and it give the same url as another box. example: click either 1 or 2 and then both 3 and 4. they both come up with the same url.
    Copy linkTweet thisAlerts:
    @scmoulJul 03.2006 — <html>

    <head>

    <script>

    url = new Array();

    url[0] = new Array();

    url[0][0] = 'http://www.gbuyforum.net/intr/1.htm';

    url[0][1] = 'http://www.gbuyforum.net/intr/2.htm';

    url[1] = new Array();

    url[1][0] = 'http://www.gbuyforum.net/intr/3.htm';

    url[1][1] = 'http://www.gbuyforum.net/intr/4.htm';

    url[1][2] = 'http://www.gbuyforum.net/intr/11.htm';

    url[2] = new Array();

    url[2][0] = 'http://www.gbuyforum.net/intr/5.htm';

    url[2][1] = 'http://www.gbuyforum.net/intr/6.htm';

    url[2][2] = 'http://www.gbuyforum.net/intr/9.htm';

    url[3] = new Array();

    url[3][0] = 'http://www.gbuyforum.net/intr/7.htm';

    url[3][1] = 'http://www.gbuyforum.net/intr/8.htm';

    url[3][2] = 'http://www.gbuyforum.net/intr/10.htm';

    function newURL(){

    d = document.forms[0];p2 =-1;p1 =-1;

    for(i=0;i<d.ch.length;i++){

    if (d.ch[i].checked){

    if(p2 == 3){

    if(i == d.ch.length-1){alert('too many boxes checked!');return false}

    else {p1 = 0;break}

    }

    else {p1 = i;p2++;}}}

    window.open(url[p1][p2],'_self');

    }

    </script>

    </head>



    <body>

    <form>

    <input name="ch" type="checkbox">1<br>

    <input name="ch" type="checkbox">2<br>

    <input name="ch" type="checkbox">3<br>

    <input name="ch" type="checkbox">4<br><br>

    <input type="button" onclick="return newURL()" value="GO URL">

    </form>

    </body>

    </html>
    Copy linkTweet thisAlerts:
    @scmoulJul 04.2006 — ...
    Copy linkTweet thisAlerts:
    @scmoulJul 10.2006 — can someone help, I am still confused on this.
    ×

    Success!

    Help @idesign123 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 5.16,
    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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...