471,316 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

Radio buttons conditional submit

Here's A piece of my code:

<form action="dimensions.php" method="POST">
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Customer" checked> Customer</br>
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Employee"> Project Manager</br>
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Vendor" > Vendor</br>
<br><input type="submit" name="submitknop" value="Change"></br>

What I want here is that when I click on the submit button, the 'form
action' target should be changed to an other webpage depending on the
radio button that has been selected. So, if I selected 'Customer',
another page should be opened than when I select 'Vendor'. I spent a
whole day on it (also trying JS etc.) and can't get it to work.
Anyone to help me?
Jul 23 '05 #1
4 2185
"Sjon" <re*****@gmail.com> skrev i meddelandet
news:41**************************@posting.google.c om...
Here's A piece of my code:

<form action="dimensions.php" method="POST">
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Customer" checked> Customer</br>
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Employee"> Project Manager</br>
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Vendor" > Vendor</br>
<br><input type="submit" name="submitknop" value="Change"></br>

What I want here is that when I click on the submit button, the 'form
action' target should be changed to an other webpage depending on the
radio button that has been selected. So, if I selected 'Customer',
another page should be opened than when I select 'Vendor'. I spent a
whole day on it (also trying JS etc.) and can't get it to work.
Anyone to help me?


1. Give the form a name attribute to be able to reference it:
<form name="myform" action="dimensions.php" method="POST">

2. Add an onsubmit handler:

// In the form tag:
<form name="myform" action="dimensions.php" method="POST" onsubmit="return
submitForm();">

// in the <head>:
<script type=text/javascript">
function submitForm(){

var theForm = document.forms["myform"];
if(theForm){
if(theForm.categorie[0].checked){
theForm.action="blah1.htm";
}
else if(theForm.categorie[1].checked){
theForm.action="blah2.htm";
}
else if(theForm.categorie[2].checked){
theForm.action="blah3.htm";
}
}

return true;
}
</script>

Untested, but you can see how it's supposed to work. (You do have a closing
</form> tag, right?)

Joakim Braun

Jul 23 '05 #2
> "Sjon" <re*****@gmail.com> skrev i meddelandet
news:41**************************@posting.google.c om...
Here's A piece of my code:

<form action="dimensions.php" method="POST">
<br><input type="radio" style="background : #84c7e8" name="categorie"
value="Customer" checked> Customer</br>

<snip>

By the way, there's no such thing as a closing </br>.

Joakim Braun
Jul 23 '05 #3
On Tue, 14 Dec 2004 10:15:56 +0100, Joakim Braun
<jo**********@jfbraun.removethis.com> wrote:
"Sjon" <re*****@gmail.com> skrev i meddelandet
news:41**************************@posting.google.c om...
[snip]
<br><input type="radio" style="background : #84c7e8"
name="categorie" value="Customer" checked> Customer</br>
These BR elements (and as Joakim said, BR doesn't have a closing tag - in
fact, it's forbidden) should be LABELs. If you styled them as

label {
display: block;
}

you should end up with the same layout, only now it's using appropriate
mark-up.

[snip]
What I want here is that when I click on the submit button, the 'form
action' target should be changed to an other webpage depending on the
radio button that has been selected. [...]
Anyone to help me?


This is really something that should be performed server-side, but I
assume from the content that this isn't for the Web anyway.
1. Give the form a name attribute to be able to reference it:
<form name="myform" action="dimensions.php" method="POST">
Why? There's no need to and if anything, it should be an id, not a name.
If you're trying to support NN4, add both.
2. Add an onsubmit handler:

// In the form tag:
<form name="myform" action="dimensions.php" method="POST"
onsubmit="return submitForm();">

// in the <head>:
<script type=text/javascript">
function submitForm(){
var theForm = document.forms["myform"];
if(theForm){
if(theForm.categorie[0].checked){
theForm.action="blah1.htm";
}
else if(theForm.categorie[1].checked){
theForm.action="blah2.htm";
}
else if(theForm.categorie[2].checked){
theForm.action="blah3.htm";
}
}
return true;
}


Or better:

function submitForm(form) {var categorie = form.elements.categorie;
if(categorie[0].checked) {
form.action = 'blah1.html';
} else if(categorie[1].checked) {
form.action = 'blah2.html';
} else if(categorie[1].checked) {
form.action = 'blah3.html';
} else {
alert('Please select a category.');
return false;
}
}

<form action="dimensions.php" method="post"
onsubmit="return submitForm(this);">

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
<OT>

Joakim Braun wrote:
"Sjon" <re*****@gmail.com> skrev i meddelandet
news:41**************************@posting.google.c om...
> Here's A piece of my code:
>
> <form action="dimensions.php" method="POST">
> <br><input type="radio" style="background : #84c7e8" name="categorie"
> value="Customer" checked> Customer</br>

<snip>

By the way, there's no such thing as a closing </br>.


By the way, there is in XHTML; however the `br' element's content model
is empty (<br/> or <br></br>).

</OT>
PointedEars
--
In theory, practice and theory are the same, but in practice they are
different -- Larry McVoy
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Owen Funkhouser | last post: by
15 posts views Thread by JR | last post: by
3 posts views Thread by Ken Varn | last post: by
1 post views Thread by IchBin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.