Forum Moderators: open

Message Too Old, No Replies

Form action on checkbox selection

         

Arno_Adams

12:21 pm on Dec 21, 2005 (gmt 0)

10+ Year Member



Hi all,

I'm no javascript wizard, but I don't understand why the following code doesn't wordk. Any Suggestions?

<SCRIPT language="JavaScript">
<!--
function OnSubmitForm()
{
if(document.pressed == 'yes')
{
document.forms[0].action ="q_5.php";
}
else(document.pressed == 'no')
{
document.forms[0].action ="q_7.php";
}
return true;
}
//-->
</SCRIPT>

<form action="q_5.php" name="form" onSubmit="return OnSubmitForm();">
yes<input type="checkbox" name="q_4" onSelect="document.pressed=this.value" value="yes">
no<input type="checkbox" name="q_4" onSelect="document.pressed=this.value" value="no">
</form>

TIA, AA

kaled

2:25 pm on Dec 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are too many mistakes... try this.

<SCRIPT type="text/javascript">
<!--
function submitForm(form)
{
if (form.q_4.checked) form.action = 'q_5.php'
else form.action = 'q_7.php';
return true;
}
//-->
</SCRIPT>
<form action="q_5.php" name="myform" onsubmit="return submitForm(this)">
<label><input type="checkbox" name="q_4" value="yes">yes</label>
</form>

Kaled.

Arno_Adams

3:52 pm on Dec 21, 2005 (gmt 0)

10+ Year Member



Hi kaled,

Thanks for your reply, but that's not what I try to achieve.

I want a form with 2 checkboxes (YES and NO).
If you select 'YES' the form posts to foo.php, if you select 'NO', the form posts to bar.php

I hope that was clear from my previous post.

AA

kaled

4:34 pm on Dec 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you require mutually exclusive boxes you should use radio boxes, however, if there are only two, a single checkbox conveys the same information (but may require a different question).

Kaled.

rocknbil

8:46 pm on Dec 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^ ^ Agreed, too often we try to force checkboxes to act like radios and radios to act like checkboxes. This is a pertfect example, you need one or the other, it is not optional, so a radio is what you want to use.

Another solution: If Javascript is disabled, create a small scriptlet to take the selected value of q_4 and direct it properly, hence the initial action in my sample is redirect.php.

Don't name a form form. form is a JS keyword referencing the current form as below. Remove the alert and this should work OK.

<script type="text/javascript">
function OnSubmitForm(form) {
form.action = (form.q_4[1].checked == true)?'q_7.php':'q_5.php';
alert(form.action);
form.submit();
}
</script>

<form action="redirect.php" name="form1" onSubmit="return false;">
yes <input type="radio" name="q_4" value="q_5.php" checked>
no <input type="radio" name="q_4" value="q_7.php">
<input type="submit" onClick="OnSubmitForm(this.form);" value="Submit">
</form>

Arno_Adams

10:20 am on Dec 23, 2005 (gmt 0)

10+ Year Member



Hi,

Thank you both for your replies. This works great!

AA