Let me try to give you some ideas ... It seems that you want your program to do at least 4 things: - Create a quiz,
- store it some where
- let someone take a stored quiz, and
- display the results
so make up a parameter (probably hidden) that gets passed to your CGI program that tells it what action you intend. Maybe even call that parameter "action". Then the main part of your program could look something like this:
#!/usr/bin/perl
use CGI qw(:standard);
my $action = param('action'); $action ||= 'TakeQuiz';
if ($action eq 'CreateQuiz') { CreateQuiz(); }
elsif ($action eq 'StoreQuiz' ) { StoreQuiz(); }
elsif ($action eq 'ShowResults') { ShowResults(); }
else { TakeQuiz(); } # default action
exit;
CreateQuiz() would present the user with a form that allows them to enter questions and answers (much the same as the code you currently have). A hidden action parameter on this form would have a value of "StoreQuiz" so that the StoreQuiz() routine can be called when you submit the form. StoreQuiz() would put the results in a file or a database or something for retrieval later. When you want to take the quiz, don't provide an action parameter and let it execute the default TakeQuiz() routine. This routine would read from where ever you stored the questions and answers and display the questions for the user to answer. A hidden parameter named "action" would be set to 'ShowResults' so that when they submitted the form, it would execut the ShowResults() routine. The ShowResults() routine would read from where ever you have the questions and answers stored, read the form data that was submitted, and produce a report that shows the user's answers and the correct answers. That's a rough design outline (primitive but workable). Now as far as implementing this stuff, just read the documentation for the CGI module. It's got nifty routines for saving form data to and retrieving it from files. And obviously it's got routines for reading the form data submitted by the user.
|