Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying result of a questionaire. 2

Status
Not open for further replies.

soonkhoo

Programmer
Dec 8, 2000
133
MY
Hi all, is there a way to use Javascript to do questionaire? For example, let's just say there are 5 questions. Each question is an individual page. And a page contains say 4 or 5 answers... doesn't matter... but how to do in such a way that after answering all of the 5 questions, the result would be, say the user got 3 right answers:--> 3/5 ? Thanks!
 
Use a hidden frame to store the results of the questions (each time they get a right answer, increment a counter):

numcorrrect++

Then, once all the questions are answered, access that variable and write it to a new page. jared@aauser.com
 
1- this is a cross post thread !
2- i find it weird to use 5 pages for 5 questions .... it's much more easy if you use just one page
3- i love the hidden frame tip !! my favorite :)
 
Actually, the answerss are in pictures, They have to click on the right one. For each question, there are pictures around, so, say i have 5 questions = tons of pictures...:)

I thought of the hidden frame before, just don't know how to do that. Jared, can you share?

Also, how do u increment the counter?
 
make a frameset with 0 rows and name it myhiddenframe ( or whatever ). then you can access it from the question page like so:

parent.myhiddenframe.numcorrect jared@aauser.com
 
<frameset rows=0,* ...>
<frame name=hidden src=....>
<frame name=main src= ...>
</frameset>

that is to create a hidden frame

now say you have &quot;numcorrect&quot; as a field in the hidden frame, to increment it :
top.hidden.document.myform.numcorrect.value++ should work

you can have numcorrect as a jscript value in the hidden frame, but jaredn is better than i am on this ;-)

 
Ahhh... I never thought of that way to hide the frame. Thanks!! Err... I'm so sorry iza, I'm very poor at this, but, you wrote

top.hidden.document.myform.numcorrect.value++

which no doubt it's true, but how do I create and array for my right answers? Let's just say, if I have 3 option buttons, and if I picked the right option, it should increment. And you were saying about numcorrect as my field, do you mean that numcorrect is the &quot;variable&quot; that I should create an array of?
 
Do you need to keep track of which ones they got correct, or just how many? jared@aauser.com
 
All I need is to display the result like:

You got 3 out of 5 right.
The 5 could be fixed and the 3 is changed depending on the number of correct answers. If the user chose the wrong choice, it will ignore judgement. Hence the right answers selected will be incremented and be displayed in the result page.
 
Then you dont need an array.

Here's your frameset

<frameset rows=*,0>
<frame name=main src=&quot;question1.htm&quot;>
<frame name=hiddenframe src=&quot;control.htm&quot;>
</frameset>

control.htm has this:

<script>
numcorrect=0
</script>

question1.htm has this (along with the question):

<script>
funciton questionAnswered()
{
if(code to see if its correct)
{
parent.hiddenframe.numcorrect++
}
self.location=&quot;question2.htm&quot;
}
</script>

then, on question5 page have this instead:

<script>
funciton questionAnswered()
{
if(code to see if its correct)
{
parent.hiddenframe.numcorrect++
}
self.location=&quot;results.htm&quot;
}
</script>

then results.htm would do something like this:

You got
<script>
document.write(parent.hiddenframe.numcorrect)
</script>/5 correct
jared@aauser.com
 
Thanks Jared,

I tried to use it... but err.. I think I might have misunderstood something... I test out just 2 questions and a result page... I managed to get 2/2 for the correct answer... but when I click the wrong answer... it still gives me 2/2. What went wrong?

This is what I did.. for the frameset file (ReviewTest.htm)
<html>
<head>
<title>Untitled Document</title>
</head>

<frameset rows=*,0>
<frame name=main src=&quot;question1.htm&quot;>
<frame name=hiddenframe src=&quot;control.htm&quot;>
</frameset>

<noframes><body bgcolor=&quot;#FFFFFF&quot;>

</body></noframes>
</html>

Then, I have the (control.htm)
<html>
<head>
<script language=&quot;JAVAscript&quot;>
numcorrect=0
</script>
<title>Untitled Document</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
</body>
</html>

Then, I have (question1.htm)
<html>
<head>

<script>
function questionAnswered()
{
{
parent.hiddenframe.numcorrect++
}
self.location=&quot;question2.htm&quot;
}
</script>
<title>Untitled Document</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<center>
<input type=&quot;button&quot; value=&quot;YES&quot; onclick=&quot;questionAnswered();&quot;>
<br>
<input type=&quot;button&quot; value=&quot;NO&quot; onclick=&quot;self.location='question2.htm'&quot;>
</center>
</body>
</html>

Then (question2.htm),
<html>
<head>
<script>
function questionAnswered()
{
{
parent.hiddenframe.numcorrect++
}
self.location=&quot;results.htm&quot;
}
</script>
<title>Untitled Document</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<center>
<input type=&quot;button&quot; value=&quot;YES&quot; onclick=&quot;questionAnswered();&quot;>
<br>
<input type=&quot;button&quot; value=&quot;NO&quot; onclick=&quot;self.location='results.htm'&quot;>
</center>
</body>
</html>

Lastly, the (results.htm)
<html>
<head>
<script language=&quot;javascript&quot;>
document.write(parent.hiddenframe.numcorrect)
</script>
<title>Untitled Document</title>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
/2
</body>
</html>

Once again thanks !!
 
it worked! But I'm missing an If statement. The way I did, people could still guess right.:( what should I do?
 
well you need to have an if statement to test and see if the answer is correct. Give me an example of your questions. jared@aauser.com
 
question 1= Is this program gonna work?
question 2= Will it ever end?
 
Example question:

What is my name?
Correct Answer: Jared

Now I don't care about case, so I'm going to convert their answer to all uppercase and test it against JARED

<input type=&quot;text&quot; id=&quot;question&quot;><button onClick=&quot;questionAnswered()&quot;>

now my questionAnswered function will look like this:

function questionAnswered()
{
if(question.value.toUpperCase()==&quot;JARED&quot;)
{
parent.hiddenframe.numcorrect++
}
self.location=&quot;results.htm&quot;
}

no if they type the correct answer, it increments numcorrect and even if they don't get it right it still goes to the enxt page jared@aauser.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top