Click here to Skip to main content
15,887,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code for a quiz and the aim of the quiz is to help people find the right phone for them. I have tried using the if statement to add results to this quiz but for that to work I would have to assign an if statement for every individual radio button. Here is my code so far:

HTML
<script type="text/javascript">
function validate() {
    check1 = false;
    check2 = false;
    check3 = false;
    check4 = false;
    check5 = false;
    check6 = false;
    check7 = false;
    check8 = false;
    check9 = false;
    check10 = false;
    for (i = 0; i &lt; 4; i++) {
        if (testform.q1[i].checked) {
            check1 = true;
        }
    }
    for (i = 0; i &lt; 6; i++) {
        if (testform.q2[i].checked) {
            check2 = true;
        }
    }
    for (i = 0; i &lt; 5; i++) {
        if (testform.q3[i].checked) {
            check3 = true;
        }
    }
    for (i = 0; i &lt; 4; i++) {
        if (testform.q4[i].checked) {
            check4 = true;
        }
    }
    for (i = 0; i &lt; 5; i++) {
        if (testform.q5[i].checked) {
            check5 = true;
        }
    }
    for (i = 0; i &lt; 4; i++) {
        if (testform.q6[i].checked) {
            check6 = true;
        }
    }
    for (i = 0; i &lt; 6; i++) {
        if (testform.q7[i].checked) {
            check7 = true;
        }
    }
    for (i = 0; i &lt; 5; i++) {
        if (testform.q8[i].checked) {
            check8 = true;
        }
    }
    for (i = 0; i & lt; 5; i++) {
        if (testform.q9[i].checked) {
            check9 = true;
        }
    }
    for (i = 0; i &lt; 5; i++) {
        if (testform.q10[i].checked) {
            check10 = true;
        }
    }
    if (check1 == false) {
        alert("You missed Question 1.");
        event.returnValue = false;
    } else if (check2 == false) {
        alert("You missed Question 2.");
        event.returnValue = false;
    } else if (check3 == false) {
        alert("You missed Question 3.");
        event.returnValue = false;
    } else if (check4 == false) {
        alert("You missed Question 4.");
        event.returnValue = false;
    } else if (check5 == false) {
        alert("You missed Question 5.");
        event.returnValue = false;
    } else if (check6 == false) {
        alert("You missed Question 6.");
        event.returnValue = false;
    } else if (check7 == false) {
        alert("You missed Question 7.");
        event.returnValue = false;
    } else if (check8 == false) {
        alert("Oops! You missed Question 8.");
        event.returnValue = false;
    } else if (check9 == false) {
        alert("You missed Question 9.");
        event.returnValue = false;
    } else if (check10 == false) {
        alert(You missed Question 10.");
        event.returnValue=false;
    }
}
// ]]></script>

<form action="result.php" method="post" name="testform" onsubmit="getResults">

<hr />

<h3>1. How much are you willing to spend on a phone per month?</h3>
<input type="radio" name="q1" value="A" /> A. £10-£15.
<input type="radio" name="q1" value="B" /> B. £15-£20.
<input type="radio" name="q1" value="C" /> C. £20-£25.
<input type="radio" name="q1" value="D" /> D. £25-£30.
<input type="radio" name="q1" value="E" /> E. £30-£35.
<input type="radio" name="q1" value="F" /> F. £35-£40.
<hr />

<h3>2. Are you good with technology?</h3>
<input type="radio" name="q2" value="A" /> A. Yes.
<input type="radio" name="q2" value="B" /> B. No.
<hr />

<h3>3. Are you looking for a simple phone?</h3>
<input type="radio" name="q3" value="A" /> A. Yes.
<input type="radio" name="q3" value="B" /> B. No.
<hr />

<h3>4. Are you looking for a modern type of phone?</h3>
<input type="radio" name="q4" value="A" /> A. Yes.
<input type="radio" name="q4" value="B" /> B. No.
<hr />

<h3>5. How big do you want the phone to be?</h3>
<input type="radio" name="q5" value="A" /> A. Big.
<input type="radio" name="q5" value="B" /> B. Medium.
<input type="radio" name="q5" value="C" /> B. Small.
<input type="radio" name="q5" value="D" /> B. I don't really mind.
<hr />

<h3>6. Do you care about the colour of the phone?</h3>
<input type="radio" name="q6" value="A" /> A. Yes.
<input type="radio" name="q6" value="B" /> B. No.
<hr />

<h3>7. Have you ever owned a mobile phone before?</h3>
<input type="radio" name="q7" value="A" /> A. Yes.
<input type="radio" name="q7" value="B" /> B. No.
<hr />

<h3>8. Do you want to be able to use the phone to get out of awkward social situations?</h3>
<input type="radio" name="q8" value="A" /> A. Yes.
<input type="radio" name="q8" value="B" /> B. No.
<hr />

<h3>9. Do you want to be able to access the app store and download apps using your phone?</h3>
<input type="radio" name="q9" value="A" /> A. Yes.
<input type="radio" name="q9" value="B" /> B. No.
<hr />

<h3>10. What happened to the last phone you owned?</h3>
<input type="radio" name="q10" value="A" /> A. I got bored of it.
<input type="radio" name="q10" value="B" /> B. It broke.
<input type="radio" name="q10" value="C" /> C. The contract ran out.
<input type="radio" name="q10" value="D" /> D. Other.
<input type="hidden" name="page" value="1" />

<center><input class="button" onclick="getResults()" type="submit" value="Submit" /> <input type="reset" value="Clear" /></center></form>
<textarea id="result">The right phone for you will be displayed here.</textarea>

Please if you have any idea how I can display different results in the text area depending on people's radio button choices without using the method I have already used please tell me how.
Posted
Updated 15-Nov-13 22:34pm
v2

1 solution

First of all to reduce that large code use jquery, my solution is with jquery,
first include jquery.js in your page first then in script block write following to validate :

C#
function validate() {
        if ($("input[name=q1]:checked").val() == undefined)
            return msg(1);
        if ($("input[name=q2]:checked").val() == undefined)
            return msg(2);
        if ($("input[name=q3]:checked").val() == undefined)
            return msg(3);

//write code for other radio button group
    }

function msg(qNo) {
        alert('You missed Question ' + qNo + '.');
        return false;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900