in reply to Organizing Form Input
Yon Monk,
Your question has me a bit confused.
Exactly what does that mean? IMHO allowing the end user to drive an application to that level is a Bad Idea™.
If I am reading between the lines correctly you are writing an application that is a quiz of some sort. Am I close?
Here's my approach for that type of an application:
First I'd create a table in a database (fill in your favorite database here) that had the questions. Such as:
Depending on if I'm asking multiple choice questions or ask straight questions witn only once answer (much harder to automate the scoring of) I'm going to create a related table with valid answers. For the sake of this discussion I will use multiple choice in my implementation. Here is the related table:create table quiz_questions ( quiz_question_id serial not null primary key, question TEXT );
create table quiz_answer( quiz_ansswer_id SERIAL not null primary key, quiz_question_id integer not null references quiz_question(quiz_question_id) on delete cascade, quiz_answer text not null, correct_answer boolean not null default 'false' );
To create the form here is a sniglet of code ( fetching things from the database is up to you to figure out).
| | Handwaving here | use strict; use CGI qw/ :all /; my $cgi = new CGI; # This hash reference contains the parameters for # the question... my $question = fetchRandomQueston(); # # This array contains hash references for the answers my @answers = fetchAnswers($question->{quis_question_id}); | | some handwaving here | print $question->{quiz_question},"\n"; print hidden("quiz_question_id", $queston->{quiz_question_id}),br; # There are better ways.. but I'm doing this way # for clarity my %labels = map { $_ -> {quiz_answer_id} => $_ ->{quiz_answer} } @answers; print radio_group( -name=> 'user_answer_rq', -values=> [ map { $_->{quiz_answer_id} } @answers ], -labels => \%labels, -linebreaks => 'true' ),br; print submit;
I've left a few details out, but you should be able to fill in the blanks by reading the man page for CGI.
Now once the person in question has selected an answer you can process it fairly easily as follows:
use CGI; my $cgi = new CGI; my $answer_id = $cgi->param('user_answer_rq'); my $question_id = $cgi->param('quiz_question_id'); my $answer=find_answer_in_db($answer_id}; if ( $answer->{correct_answer} ){ | process accordingly } else { | ditto }
That oughta give you a good start...
|
|---|