dooberwah has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone,
I've been writing a Quiz game with CGI.pm to run on my website and am having some problems checking the answers. The questions ask change randomly every time so I'm trying to pass the question and the answer to the page so when it reloads it has something to compare it to. I've written some code (included below) that gives some odd output and I couldn't find the bug in it.

my $match; if ($last_question eq $answer) { $match = 'yes'; } print header, start_html( -title => 'Esperanto Quiz' ), h1('Esperanto Quiz'), p( b($question) ), p( $match ), p( $last_question, $answer, ), start_form, textfield( -name => 'answer', -size => 40, -maxlength => 40 ), br, submit, hidden ( -name => 'last_question', -value => $lq, ), end_form, end_html;
The problem is that when I try to print out the $last_question and the $answer variable the $last_question seems to stay the same. They also don't match in the if ( $last_question eq $answer ) statment. Hope someone else can find the bug in this because I've been having a lot of trouble with it. Thanks.

-Ben Jacobs (dooberwah)
one thing i can tell you is you got to be free
http://dooberwah.perlmonk.org

Replies are listed 'Best First'.
Re: Checking Quiz Answers
by merlyn (Sage) on May 26, 2001 at 19:31 UTC
Re: Checking Quiz Answers
by chipmunk (Parson) on May 26, 2001 at 21:28 UTC
    After reviewing the docs in CGI, I see two problems with your use of hidden(). Here's what the docs have to say:
    CREATING A HIDDEN FIELD print $query->hidden(-name=>'hidden_name', -default=>['value1','value2'...]); -or- print $query->hidden('hidden_name','value1','value2'... +); hidden() produces a text field that can't be seen by the user. It is useful for passing state variable information from one invocation of the script to the next. Parameters: 1. The first argument is required and specifies the name of this field (-name). 2. The second argument is also required and specifies its value (-default). In the named parameter style of calling, you can provide a single value here or a reference to a whole list Fetch the value of a hidden field this way: $hidden_value = $query->param('hidden_name'); Note, that just like all the other form elements, the value of a hidden field is "sticky". If you want to replace a hidden field with some other values after the script has been called once you'll have to do it manually: $query->param('hidden_name','new','values','here');
    So, the first problem is that hidden() doesn't have an argument called 'value'; it has 'default' instead. The second problem is that the default only applies if param('last_question') doesn't already have a value.

    Here's how to get the result you want; set param('last_question') to the desired value first, and then call hidden():

    param('last_question', $last_question); print header, # ... hidden('last_question'), #... end_html;
Re: Checking Quiz Answers
by Vynce (Friar) on May 26, 2001 at 18:44 UTC

    i don't think you've given enough details for the question you've asked -- what sets $last_question?

      Sorry, there was a typo on my part.

      This:

      hidden ( -name => 'last_question', -value => $lq, ),

      Should Be This:

      hidden ( -name => 'last_question', -value => $question, ),

      -Ben Jacobs (dooberwah)
      one thing i can tell you is you got to be free
      http://dooberwah.perlmonk.org