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

Dear Monks

I'm fairly new to perl CGI, and am trying to write a simple cgi based calendar. I've run into a problem trying to update the value of a hidden field in a self-referring url. Here's a minimal example:

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; use CGI; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI; print $q->header; print $q->start_html; my $serverRoot = "$ENV{SERVER_NAME}"; my $previousTotal = $q->param('sumTotal'); print $q -> h2("Previous Total: $previousTotal<br>"); my $currentTotal = $previousTotal + 2; print $q -> h2("Current Total: $currentTotal<br>"); print $q -> start_form( -name => 'addition', -method => 'GET', -enctype => &CGI::URL_ENCODED, -action => "http://$serverRoot/cgi-bin/sum.cgi" # <-se +lf url; ); print $q -> hidden( -name => 'sumTotal', -value => "$currentTotal" ); print $q->submit( -name => 'button', -value => 'Add 2', ); print $q ->end_form; print $q ->end_html;

I thought that each time the button is pressed, the value of $currentTotal should increase by 2. After pressing the button once, $currentTotal increases to 4, but when the button is pressed again, -sumTotal is still set to 2 in the url, and not 4 as I expected.

If anyone could explain why, or direct me towards an answer, I'd be very grateful!

Thanks in advance

Michael

Replies are listed 'Best First'.
Re: perl CGI: updating hidden field values for self-referring url
by Your Mother (Archbishop) on Feb 17, 2014 at 19:35 UTC

    tangent already solved it for you. Here's a recap with some style changes and security amendments.

    use strict; use warnings; # Uninitialized errors are normal for this kind of CGI. no warnings "uninitialized"; # LWP was not being used. use CGI; # Do not leave this in the script production. use CGI::Carp qw(fatalsToBrowser); # ->new is better; search indirect object syntax problems. my $q = CGI->new; print $q->header, $q->start_html; my $previousTotal = $q->param('sumTotal'); # Printing raw user input is a serious security problem. # Adding 0 will numify and clobber strings like "<script />" print $q->h2("Previous Total:", $previousTotal + 0); my $currentTotal = $previousTotal + 2; # Don't need "print" for every op and please do not put "br"s # everywhere for formatting. print $q->h2("Current Total:", $currentTotal), $q->start_form( -name => 'addition', -method => 'GET' ), $q->hidden( -name => 'sumTotal', -override => 1, -value => $currentTotal ), $q->submit( -name => 'button', -value => 'Add 2' ), $q->end_form, $q->end_html; exit 0;
      To solve "indirect object syntax problems", use
      my $q = 'CGI'->new; # my preferred
      or
      my $q = CGI::->new;

      The common CGI->new is still ambiguous.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        I actually learned that from tobyink awhile back but have had trouble getting used to it as a style… it feels so pedantic, I look over my shoulder to see if anyone was watching whenever I type it. :P

        To solve "indirect object syntax problems", use

        Don't use single all-caps words as sub names, and you never run into the problem :)

Re: perl CGI: updating hidden field values for self-referring url
by tangent (Parson) on Feb 17, 2014 at 19:24 UTC
    CGI preserves the values in the query string when regenerating a form. You can turn this behaviour off for the whole form using the '-nosticky' pragma or for individual form elements using the '-override' attribute. Try changing to:
    print $q -> hidden( -name => 'sumTotal', -value => "$currentTotal", -override => 1, );
Re: perl CGI: updating hidden field values for self-referring url
by mtmcc (Hermit) on Feb 17, 2014 at 20:14 UTC