Click here to Skip to main content
15,885,366 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.I really need to know how I can add a function like I described to getResults().


JavaScript
<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 < 4; i++) {
        if (testform.q1[i].checked) {
            check1 = true;
        }
    }
    for (i = 0; i < 6; i++) {
        if (testform.q2[i].checked) {
            check2 = true;
        }
    }
    for (i = 0; i < 5; i++) {
        if (testform.q3[i].checked) {
            check3 = true;
        }
    }
    for (i = 0; i < 4; i++) {
        if (testform.q4[i].checked) {
            check4 = true;
        }
    }
    for (i = 0; i < 5; i++) {
        if (testform.q5[i].checked) {
            check5 = true;
        }
    }
    for (i = 0; i < 4; i++) {
        if (testform.q6[i].checked) {
            check6 = true;
        }
    }
    for (i = 0; i < 6; i++) {
        if (testform.q7[i].checked) {
            check7 = true;
        }
    }
    for (i = 0; i < 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 < 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>


HTML
<form action="result.php" method="post" name="testform"  önsubmit="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"  önclick="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>
Posted
Updated 30-Nov-13 11:29am
v3
Comments
Karthik_Mahalingam 30-Nov-13 11:33am    
please add your code in javascript code block
Karthik_Mahalingam 30-Nov-13 12:18pm    
note these points in javascript..

dont use else if, if u r using different object to check the values

use var to declare objects
Sergey Alexandrovich Kryukov 30-Nov-13 19:05pm    
This code is something opposite to programming, defeating the whole purpose of it. You are repeating the same fragments of code 10 times. If you don't through it out, forget about programming, you are not doing anything useful, it's only destructive. Start from scratch, this time not repeating anything from the set of 10, not even if blocks: it should be replaced with just on line of code.
—SA
Kornfeld Eliyahu Peter 1-Dec-13 3:44am    
I believe, we can accept a two-line solution. After all he is a new-comer! :-)
Sergey Alexandrovich Kryukov 1-Dec-13 12:06pm    
I understand. Then it's even more important not to keep the illusion that this is programming.
—SA

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