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

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; } } }

Replies are listed 'Best First'.
Re: HTML Form retaining values
by hardburn (Abbot) on Oct 03, 2003 at 16:43 UTC

    This sounds like something that could be browser-specific.

    Also, you should try using CGI.pm instead of parsing your params by hand. You could replace your ParseForm() sub with this:

    sub ParseForm { use CGI qw(:standard); my @keys = param(); foreach my $key (@keys) { $formdata{$key} = param($key); } }

    This is much safer, as CGI.pm is smart enough to avoid edge cases that could cause security problems in your CGI.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      This would do the same
      use CGI qw(Vars); my %formdata = Vars();

      - Tom

Re: HTML Form retaining values
by jdtoronto (Prior) on Oct 03, 2003 at 16:44 UTC
    Can you explain what you are trying to do a little more clearly please?

    • 1. Please use strict
    • 2. Far better to use CGI module than parse it all in code.
    • 3. I don't see any code to do an addition at all!
      As stated in the original post, this is just a sample. I have another much more detailed script that does the addition. This is just a demo of the problem.

      As for strict, I have never had good luck with it. I know what everyone says, but I chose not to use it for my simple scripts.

      Regarding the CGI module, I have no idea why it wasn't oroginally used as I am modifying code that someone else wrote.

      Again, any assistance is definetly appreciated.

        I just wanted to let you know that it is well worth persevering with strict. It is a real pain when you start and the temptation to switch it off is almost irresistable

        However it does become easier as you get used to it and the main advantage for me is that when you get to the end of it you have a script which is more robust than might otherwise be the case. Sure, your script without strict might work well for what you have in mind, but that is often at variance with what happens out in the field, and odd, unexpected things can happen if your script isn't tight enough.

        And I haven't even mentioned taint... :)

        As for strict, I have never had good luck with it. I know what everyone says, but I chose not to use it for my simple scripts.
        Then you should really stop asking for help from other people, cause the first thing they'll all keep telling you is to use strict, because strict help you avoid dumb mistakes.
Re: HTML Form retaining values
by Hagbone (Monk) on Oct 03, 2003 at 21:37 UTC
    >>>>>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.<<<<<<

    hmmmm - so what you're saying is that when you first run the script that displays the form, the display shows:

    <input type='checkbox' name='CheckBox' value=''>
    and after you run the script, the display shows:
    <input type='checkbox' name='CheckBox' value='ON'>
    ?????? ..... if that's the case, then you have something going on that you're not sharing with us, me thinks.

    And FWIW, when I work with dynamic form stuff (and more specifically, checkboxes), I'm looking to create displays that end up using "checked" to control the display .... after all, my understanding is that it's the "checked" aspect that controls wheather the submission "sends" the value to the script - like:

    <input type='checkbox' name='CheckBox' value='ON' checked>
Re: HTML Form retaining values
by mkahn (Beadle) on Oct 03, 2003 at 21:31 UTC
    There is some kind of meta tag you can use to effect when a page is loaded from the server, and when from the cache. But the relevant (counter.pl) code is missing from your post, immho.
Re: HTML Form retaining values
by jeffa (Bishop) on Oct 04, 2003 at 14:19 UTC
    As you can see, handling CGI by hand is a waste of time. CGI.pm is your friend:
    use strict; use warnings; use Data::Dumper; use CGI qw(:standard Vars); print header, start_html('sticky checkbox test'), start_form, checkbox('foo',undef,'ON','Test Check Box'), br, submit('Submit'), reset, end_form, end_html, ; if (param('Submit')) { print pre(Dumper {Vars}); }

    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: HTML Form retaining values
by bean (Monk) on Oct 04, 2003 at 01:35 UTC
    This is how modern versions of Mozilla and IE behave for some reason - it may have something to do with page history. Changing the name of the form element will clear the value - although that creates new problems, it suggests that renaming the form might work too (usually nobody cares about form names so it's a better solution). Changing the name of the page will do it too, and GET vars count for that (I think). So just changing from POST to GET could solve your problem. Of course, you could always use javascript - I wrote something generic enough to work for just about all forms and javascript1.1 browsers at one point, but don't have it on me right now...