I'm trying to write a multi question survey in perl that displays one question at a time with a "previous" and "next" button. Eventually I will need to read the questions from a file, but I haven't gotten that far yet so I am hard coding them in for now.
Part of the assignment requirements are that I MUST use CGI, so I cannot print any html directly.
Currently I have the script printing out the first question, along with two submit buttons, one labeled 'next' and the other 'previous'.
print $form->submit(-name=>'question', -value=>'next');
print $form->submit(-name=>'question', -value=>'previous');
I also have a hidden field:
print $form->hidden(-name=>'hidden', -value=> $currentQ);
My idea is that once next is clicked, I would increment (or decrement, if previous was clicked) $currentQ so that the script knows which question it was on.
The problem I'm having is manipulating the hidden field once the button is pushed. I have:
my $direction = $form->param( 'question' ) || '';
if ($direction eq 'next'){
$currentQ++;
}
Along with a print statement to print $currentQ. In other words, it should print a higher number each time I click next, but all it does is remain at 1(This is just to test the functionality, once this is working I then have to figure out how to actually implement it so that it will print the correct question).
Hopefully this description makes some sense, but if you need more details please let me know. I开发者_开发问答'm really stuck on this one, so any help is greatly appreciated, thanks in advance!
You have an inconsistency; you are naming the hidden field "hidden", but are getting the value from the parameter "question". I don't know if this is present in your actual code.
One gotcha with using CGI is that the value passed to the input-field producing methods is just a default value; a value supplied with the request takes precedence:
use CGI;
$form = CGI->new("hidden=41");
print $form->hidden(-name=>'hidden', -value=>42);
prints
<input type="hidden" name="hidden" value="41" />
To change this, you either need to supply the -override parameter:
print $form->hidden(-name=>'hidden', -value=>42, -override=>1);
or change the stored value of the parameter:
$form->param('hidden',42);
print $form->hidden(-name=>'hidden', -value=>42);
Make sure your "if next, increment" logic is executed before you call the hidden method to generate the html.
精彩评论