I have a simple bit of code below that I am using as a cgi script. When the code loads, you see the form as I intended. If you put a check in the Test Check Box and then click the submit button, you will then see the key/value pairs present in the form. That is all fine. Once the submit button is clicked, you will notice that the check box is then unchecked. Again, all is fine with that. However, when you click reload in your browser, the check box remains unchecked however the value of the checkbox variable remains the same (ON in this case). I am searching for a way to reload the page such that the value is empty, as it is when you first run it. In other words, when the check box clears, I wish for the variable to be cleared as well instead of maintaing it's value and being printed again on the screen.

Obviously this is just a test script, but it demonstrates a problem I am having in a larger script. That script has a counter that does addition when a check box is checked. If the check box is checked and the submit button is clicked it does the addition but it also does the same addition each time the reload button is checked.

How can I prevent this behavior?

See sample below.
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "<form method='POST' action='/cgi-bin/demo.cgi'>\n"; print "Test Check Box <input type='checkbox' name='CheckBox' value='ON +'></p>\n"; print "<input type='submit' value='Submit' name='Submit'><input type=' +Reset' value='Reset' name='B2'></p>\n"; &ParseForm; foreach $key (sort keys(%formdata)) { print "KEY=$key<br>\n"; print "VALUE=$formdata{$key}<br><br>\n"; } sub ParseForm { if ($ENV{'REQUEST_METHOD'} eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); } else { print "Content-type: text/html\n\n"; print "<P>Use Post or Get"; } foreach $pair (@pairs) { ($key, $value) = split (/=/, $pair); $key =~ tr/+/ /; $key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg; $value =~s/<!--(.|\n)*-->//g; if ($formdata{$key}) { $formdata{$key} .= ", $value"; } else { $formdata{$key} = $value; } } }

In reply to HTML Form retaining values by Anonymous Monk

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.