Search code examples
perlformscgisurvey

Displaying survey question 1 at a time


I'm writing a survey that should display each question one at a time. I'm a little rusty on how exactly forms work, so that may be the issue but here's my question.

The program reads the questions from a file and displays them on the page. Essentially I want to use a form to display a question, subit the answer via a next or previous button, and go to the next question from there.

Worth noting is that I must use CGI (although I don't think this effects much).

From what I understand I have to use some sort of hidden field to keep track of the current question number, is this accurate?


Solution

  • From CPAN there is a module called CGI::Session. This module handles, as you might guess, session management. In so doing, it facilitates maintaining state in your CGI applications. Unless your needs take you an a direction that is specifically not covered by CGI::Session, you're well-advised to let it do the heavy lifting on session/state management for you.

    While it is possible to pass information from one state to the next using hidden fields, there are ways to do it that are more reliable than others. And hidden fields are only one of several techniques. GET requests may pass session info from one request to the next. Hidden fields as well. Cookies too. But rather than passing the current and next question number, it's often better to pass a hash value that identifies a particular session. That is harder for someone to manipulate in ways that your script doesn't intend to deal with. The point to this paragraph is that CGI::Session lets you not worry so much about the implementation of your session management. That frees you up to concentrate on getting something done (the primary purpose of the web application).