Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I sum up answers on an HTML Web-based form?

Dave, I just want to add numbers together, get an answer, and retrieve a document represented by the answer. I have 11 pages for people to answer "yes" or "no" on. I have my "yes" and "no" buttons working properly. What I need to do is assign numerical values to each button on each page. Then I need to add the values of all 11 pages of each button pushed to get one answer. I want to use this answer to retrieve one document to be displayed for the viewer. Maybe you know of some tricks that can be used in HTML to do this. I was thinking if I can't do it in HTML, maybe javascript might be an answer.

Dave's Answer:

You pose an interesting question. The easiest way to solve this, I think, is to produce a small CGI script that modifies each of the 11 pages as sent so that they're actually all forms. Then the answers to each question are simply the sum value to that point in the 11 questions plus the point value for that particular answer to the question.

This type of approach could be used to ascertain a Myers-Briggs personality profile, for example, where a set of questions would be used to determine which of each possible alternative pair applies best to the visitor (e.g., "introvert", or "extrovert", etc).

For example, if your fourth question is "Swallows are the fastest bird" and the two possible answers are "yes" (worth 2 points) and "no" (worth 1 point), and the person is going into this question with a sum value of 7 points, it could be structured thusly:

Swallows are the fastest bird<br />
<input type="radio" name="ans4" value="9"> yes <br />
<input type="radio" name="ans4" value="8"> no <br />

See what I'm getting at here? Your CGI script would look for values to ans1, ans2, ans3, etc., through 11, to figure out what page the visitor's on, then look at the value of that variable to ascertain the sum score to that point.

When the CGI script sees "ans11" then the value of "ans11" is actually the final sum value of the survey and you could easily have solution pages that are simply named after their numeric value, as in 1.html, 2.html and so on.

You could also do this in a number of different sophisticated ways, including using JavaScript to step through all the questions without ever leaving the Web page and touching your server. This has the advantage of being much more efficient in terms of server use, but it'll be considerably harder to program and, possibly, modify to fix typos, re-weight questions, etc.

Yet another approach would be to use a sophisticated combination of JavaScript and Cascading Style Sheets, where all the questions would be loaded at once, but only the current question would have its "visibility" set to true. Again, more efficient in terms of your server load, but more difficult to program correctly.

I hope this gets you traveling in the right direction. Good luck!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments
Rather amazingly, there are no comments on this article yet.

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]