http://www.perlmonks.org?node_id=189961

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

Here is the code:
#!perl -Tw use CGI qw(:standard); use strict; my $num = param('number') || 0; print header, start_html; print "Your number is $num."; print start_form; print hidden('number', $num + 1); print submit('Get next number'); print end_form; print end_html;
The first time this is run, it prints "Your number is 0" and the hidden field is set to 1, as expected. But when I click on the button and it calls count.cgi?number=1, it prints "Your number is 1" but the hidden field is again set to 1! Why isn't it going to 2?

--MrNobo1024
s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

Replies are listed 'Best First'.
Re: Problem with CGI.pm generated forms
by Hero Zzyzzx (Curate) on Aug 14, 2002 at 01:21 UTC

    Mayhap you should check out the "-override" option? By default, form values are sticky, and you need to set them explicitly how jeffa did above, or by passing them to your method and setting "-override" to a true value.

    #!/usr/bin/perl + use CGI qw(:standard); use strict; my $num = param('number') || 0; print header, start_html; print "Your number is $num."; print start_form; print hidden(-name=>'number', -value=>$num + 1,-override=>1); print submit('Get next number'); print end_form; print end_html;

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

(jeffa) Re: Problem with CGI.pm generated forms
by jeffa (Bishop) on Aug 14, 2002 at 01:16 UTC
    Very common pitfall ... you need to also set the value with the param() method:
    param('number',$num + 1); print hidden('number', $num + 1);
    I can't remember why off hand though ...

    UPDATE: LOL - you think i would have known that from the advice i gave at (jeffa) Re: Unstickying CGI ... Hero Zzyzzx++

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)