/    Sign up×
Community /Pin to ProfileBookmark

form link to send to…

Hi Guys,
I am not at a high level for integrating javascript into forms.

I have built a form which only has two options with each option having two check boxes. I want the form to go to a specific link depending on which check boxes have been selected.

Is there a simple way to do this with JS?

Any help is hugely appreciated.

Regards Bruce

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@russellDec 21.2004 — Something like this ought to do the trick, though you need to have a default action in case JS isn't enabled.
<i>
</i>&lt;script&gt;&lt;!--
function setAction(f) {
if(f.ck1.checked==true) f.action = "page1.html"
if(f.ck2.checked==true) f.action = "page2.html"
}
// --&gt;
&lt;/script&gt;
&lt;form name=f&gt;
&lt;input type=checkbox name=ck1 value=1&gt;
&lt;input type=checkbox name=ck2 value=2&gt;
&lt;input type=submit onclick="setAction(this.form)"&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@CharlesDec 21.2004 — Remember that one in ten users do not use JavaScript so you'll need a default, no-JavaScript e-mail address for those folks.

[font=monospace]<form action="[email protected]">[/font]

And be careful. Not all versions of HTML allow the FORM element to take a "name" attribute.
Copy linkTweet thisAlerts:
@BruscaauthorDec 21.2004 — Thanks for your help guys. I managed to get that working but now have had to make some changes to radio buttons. Could not get it to work on radio buttons.

I tried it but did not work.

<script><!--

function setAction(f) {

if(f.type.checked.value==1) f.action = "http://www.dummyaddress.com"

if(f.type.checked.value==2) f.action = "http://www.dummyaddress2.com"

}

// -->

</script>

<form name=f>

<input name="type" type="radio" value=1>

<input name="type" type="radio" value=2>

<input type=submit onclick="setAction(this.form)">

</form>
Copy linkTweet thisAlerts:
@cyber1Dec 21.2004 — You deleted the names of the checkboxes.

For radio buttons, change only the word 'checkbox'

<input type=checkbox name=ck1 value=1>

<input type=checkbox name=ck2 value=2>


-bill
Copy linkTweet thisAlerts:
@BruscaauthorDec 22.2004 — Ok no worries,

So I have 4 radio buttons.

Payment Method (only one radio button selected)

(first radio button) Credit Card

(second radio button) Direct Debit

Payment Type (Only one radio button selected)

(first radio button) Recurring Payment

(second radio button) One off payment

Submit button

I know this is a lil different to what is above. But depending which radio buttons are selected from each choice the url needs to be diff for each case.

Its not my choice for doing it this way but my hands are tied.
Copy linkTweet thisAlerts:
@compbrat75Dec 22.2004 — Hi Brusca,

If you are using Radio Buttons, try this:

[code=php]
<html>
<head>
<script language="JavaScript">
<!--
function setAction(f) {
var method = document.f.paymethod;
var type = document.f.paytype;
var methchoice = 0;
var typechoice = 0;

//get the value of each radio group
for(var i=0; i<method.length; ++i) {
if(method[i].checked) {
methchoice = method[i].value
}
}
for(var j=0; j<type.length; ++j) {
if(type[j].checked) {
typechoice = type[j].value
}
}

//set the action for the form based on the selection
if((methchoice == "Credit") && (typechoice == "Reoccuring")) {
f.action = "mypage1.cgi"
}
if((methchoice == "Credit") && (typechoice == "One_Time")) {
f.action = "mypage2.cgi"
}
if((methchoice == "Debit") && (typechoice == "Reoccuring")) {
f.action = "mypage3.cgi"
}
if((methchoice == "Debit") && (typechoice == "One_Time")) {
f.action = "mypage4.cgi"
}
}

// -->
</script>
</head>
<body>
<form name="f" method="post" action="">
Credit:<input type="radio" name="paymethod" value="Credit">
Debit:<input type="radio" name="paymethod" value="Debit">
<br>
Reoccuring:<input type="radio" name="paytype" value="Reoccuring">
One Time:<input type="radio" name="paytype" value="One_Time">
<input type="submit" onclick="setAction(this.form)">
</form>
</body>
</html>
[/code]

Hope this helps. ?

~compbrat75
Copy linkTweet thisAlerts:
@BruscaauthorDec 22.2004 — Thanks heaps thats a big help!!
×

Success!

Help @Brusca 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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