Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q415 How can I vet radio buttons so that if all the answers are 'yes' the form loads another page, but if even one answer is 'no' the form results are passed to a cgi script instead?

You are here: irt.org | FAQ | JavaScript | Form | 3.4 | Q415 [ previous next ]

Try something like:

<form name="myform" action="success.htm" onSubmit="return vetAnswers()">
    Yes <input type="radio" name="q0">  No <input type="radio" name="q0">Is the Sky Blue?
<br>Yes <input type="radio" name="q1">  No <input type="radio" name="q1">Is the Grass Green?
<br>Yes <input type="radio" name="q2">  No <input type="radio" name="q2">Is the Ocean Blue?
<p>
<input type="submit" value="Submit Application">
</form>

<form name="failed" action="faild.cgi">
<input type="hidden" name="Is the Sky Blue?">
<input type="hidden" name="Is the Grass Green?">
<input type="hidden" name="Is the Ocean Blue?">
</form>

<script language="JavaScript"><!--
function vetAnswers() {
    var yes = 0;
    var no = 0;
    for (var i=0;i<3;i++) {
        if (eval("document.myform.q" + i + "[0].checked") == true) {
            yes++;
            document.failed.elements[i].value = 'yes';
        }
        if (eval("document.myform.q" + i + "[1].checked") == true) {
            no++;
            document.failed.elements[i].value = 'no';
        }
    }
    if (yes == 3) {
        return true;
    }
    else {
        alert('Sorry, you just failed the Test');
        document.failed.submit();
        return false;
    }
}
//--></script>

Feedback on 'Q415 How can I vet radio buttons so that if all the answers are 'yes' the form loads another page, but if even one answer is 'no' the form results are passed to a cgi script instead?'

©2018 Martin Webb