Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Problem with CGI.pm generated forms

by MrNobo1024 (Hermit)
on Aug 14, 2002 at 00:36 UTC ( #189961=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Problem with CGI.pm generated forms
Download Code
(jeffa) Re: Problem with CGI.pm generated forms
by jeffa (Chancellor) 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)
    
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://189961]
Approved by osfameron
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-07-30 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls