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

In reply to perl CGI: updating hidden field values for self-referring url by mtmcc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.