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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display result of a questionaire.

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, and the display would be:--> 3/5 ? Think of it like a course rather than option buttons for surveys.

Thanks.
 
I can see one flaw (which you may be able to get around - haven't thought about it much) in that if you are going to do this in JavaScript, then the answers must be coded on the page, so that someone can View Source and get the answers????????????

Simon
 
That's true... but it's ok... it's not a very tough question anyway. It's something that the user should be able to answer. Oh, can't you create a .js file and hide it in there instead?
 
If you want to be able to record the answers that people choose, then you'll have to use a server-side script to write the answers to a file or database.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
After the first question, post the answer to your next question page. On question 2, post the answer to 1 (in a hidden input box) and question 2. On question 3, post the answers to 1 and 2 (in hidden input boxes) and question 3. ......
AFTER the last question, on your checking page, you can then validate the answers and give the score.

Simon
 
this thread is a cross post one, and it has been answered (full code !!) in the jscript forum ...
 
tanderso,

Why does the thread have to be "Red Flagged - Offensive" if it has already been discussed?? I do not find it offensive if subjects come up more than once. All that it needs is iza's post to point us in the right direction.

iza,

Do you know which thread is was?? Can you post the link - If you do not know straight away without lengthly searching, then I will look.

Thanks,

Simon
 
I'm not sure what's been offered in the other forum, but here is a way to do it with pure javascript:

Each of your pages has a series of questions:

<input type=&quot;text&quot; name=&quot;one&quot;>
<input type=&quot;text&quot; name=&quot;two&quot;>

...

Then finally a button to go the next set of questions:

<input type=&quot;button&quot; onClick=&quot;goToNextPage('next_page.htm',this.form,'');&quot;>

The goToNextPage() function could be placed in a .js file that will be included by all pages and would be something like this:

/*** Untested ***/

function goToNextPage(url,f,data)
{
url+=&quot;?&quot; + data;

for (var i=0;i<f.elements.length;++i) {
// This assumes that all answers are
// in text boxes, so this
// will have to be modified to allow
// for radio buttons, selection lists etc.
if (f.elements.type==&quot;text&quot;) {
url+=url.charAt(url.length-1)==&quot;?&quot; ? f.elements.name + &quot;=&quot; + f.elements.value :
&quot;&amp;&quot; + f.elements.name + &quot;=&quot; + f.elements.value;
}
}
self.location.href=url;
}

This would send you to the next page of questions with the questions and answers from the page appended to the URL. Each page after the first page would need to call a function (again, it can be defined in a common .js file) that grabs the questions and answers from the URL:

function parseURL()
{
s=self.location.href;
return s.indexOf(&quot;?&quot;) ? s.substring(s.indexOf(&quot;?&quot;)+1) : &quot;&quot;;
}

The return from this can be stored in a global variable:

<body onLoad=&quot;data=parseURL();&quot;>

Then, on this page (the 2nd page and later), you would call the goToNextPage function like this:

<input type=&quot;button&quot; onClick=&quot;goToNextPage('next_page.htm',this.form,data);&quot;>

And so on...

Eventually, the user will get to the final page (note that the answers to the questions haven't appeared in any files the user has visited up to this point). The final page will call parseURL to grab the block of questions and answers and split them up into question/answer pairs and then compare them against the correct answers which will be hard coded on this page. This will occur in the onLoad() event and the results will be written on the final page. Yes, the user can still get at the answers, but this method encourages the user to stay honest as he/she moves through the series of questions.

I'll leave the question/answer parsing as an exercise to the reader as I'm about to go to sleep :)

An alternative to storing the answers in the URL is using cookies which I think would be preferable, but I thought a quasi-form GET javascript would be interesting.

If you really want to keep the answers from the user and do a *real* questionairre, what Tom said is true: use server side scripting. Besides, you can also store users' answers in a database and do all kinds of other neat stuff that you can't do with client-side scripting.

Russ

Russ
bobbitts@hotmail.com
 
It should be red-flagged so that people's efforts are not redoubled on a question which has already been answered. If you prefer, post a link to the other thread. But, I would just red-flag it to keep the clutter away.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
i red flagged it (not only to please tom, mainly because it's pretty annoying to take time to answer a question, and then see that question already answered somewhere else ! also because i feel that cross posting is lazyness, to me it means that you didn't focus enough on the question to know what kind of problem you have ... just the same as posting the whole page code ... or asking a question that has already been answered loads of time, it only shows that you are too stupid or to lazy to use the search function)
btw i was wondering if i can red flag all those &quot;this is my code where is the error&quot; and then 1 page of rotten code; or the &quot;please how do i do a frameset&quot; and when you answer, obsiously, the guy has never read any doc, never heard of html, he is just waiting for you to work for him (such as thread215-42863 or thread232-42179))
 
You can 'red flag' any posts that you want I suppose. My interpretation of the purpose of the red flag option is to alert the moderators about a post that is /blatantly/ offensive (e.g. someone making personal insults, spam etc.).

I agree that some of the posts that you mention where someone just wants a free ride /may/ fall into this category also as this would seem to go against the spirit of the forum.

However, I can't see any reason why the post by swilliams should be red-flagged. There isn't any indication that he knew when he was posting that an answer had already been given in another forum at the time of his post. Even if he did know, I would still disagree that it should be red-flagged and is a waste of time. Several perspectives on a problem can often contribute to a better solution and improve everyone's knowledge.

Russ
bobbitts@hotmail.com
 
maybe swilliams shouldn't be red flagged, but it's really really often that i answer a question, and then go to another forum, and find that question already answered. I mean i'd have prefer to answer on the already started thread, this is easiest to compare solutions and see all the differents ways to solve the problem, and while discussing, finding a new way, instead of having to move from one forum to another. What cross posters should do is warn us &quot;ok, this is a cross post thread, i'm not sure if it's more of a jscript or dhtml problem, so i post in both&quot; - at least we know ! or tek-tips should find a way to have cross-forums threads ?
anyway i guess that tektip admins won't allow me to red flag this one !!!
 
Russ, Iza, Tom, Simon and Soonkhoo:

Regarding the Red Flag utility you are all correct!

We do allow cross posting on the site. However, cross posting you see in more than two forums should be red flagged.

If you see a cross post already answered in another forum, definitely red flag the other cross post especially if it hasn't been responded to.

If you see a cross post being answered in two forums, simply post the thread number as a cross reference to &quot;hopefully&quot; move everyone interested into one place.

We absolutely want members to use the red flag as a productivity tool to remove duplicate posts in addition to offensive material in the forums.

Thank you to you all. :)

Dave Murphy (aka Hunter)
dmurphy@tecumsehgroup.com

 
Oops! Didn't know that could happened...I'm sorry for what had happened...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top