Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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?

irt.org | Knowledge Base | JavaScript | Form 3.4 | Q415 [ previous next ]

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?

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.