Click to See Complete Forum and Search --> : Form action depending on which radio button is checked


Lotz
September 1st, 2003, 02:34 PM
Is it possible to have a form perform a different action depending on which radio button is checked?

For example, if I have 2 radio buttons "under 13 years" + "over 13 years", I would like that if under 13 is checked, the action is /cgi-bin/under13.cgi and if over 13 is checked, the action is /cgi-bin/over13.cgi.

It is working with a drop down list, put I would like to put radio button instead.

<SCRIPT LANGUAGE="JavaScript"><!--
function setAction() {
document.form.action = document.form.select.options[document.form.select.selectedIndex].value;
}
//--></SCRIPT>

Could you help me to configure it with radio buttons?

Thanks a lot

khp
September 1st, 2003, 05:56 PM
Well, normally one would use the radio buttons to set the value of a variable, and then let the cgi script, send a different response depending on the value of that variable. But if you insists on doing it in javascript, you could do something like this.


<html> <head>
<script language="javascript">
function setAgeFormAction(url) {
p = document.getElementById("ageform");
p.setAttribute("ACTION",url);
} </script></head>
<body>
<FORM METHOD=get id="ageform" ACTION="javascript:alert('You havent selected your age')">
<INPUT TYPE=radio NAME=age VALUE=12 onclick="javascript:setAgeFormAction('/cgi-bin/under13.cgi')"> Under 13
<INPUT TYPE=radio NAME=age VALUE=14 onclick="javascript:setAgeFormAction('/cgi-bin/over13.cgi')"> Over 13
<INPUT TYPE=submit VALUE="Submit">
</FORM>
</body></html>