in reply to How to get unselected radio_group() items after submitting form

The most likely culprit if your problem is the end_form function as demonstrated below.

I created the following cgi script to test your issue

#!/usr/bin/perl -wT use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use strict; use warnings; my $q = CGI->new; print $q->header(); print <<"END_HTML"; <html> <body> <form method="post"> END_HTML foreach my $f (qw(foo bar baz)) { print "<p>$f</p>\n"; print $q->radio_group( -name => $f, -values => [qw(c r t s)], -default => '-', ) . "\n"; } print $q->submit(-name => 'submit', -value => 'save') . "\n"; print <<"END_HTML"; </form> </body> </html> END_HTML 1; __END__

This appears to work as you desire. You can hit submit multiple times, and it will only autofill the radio buttons that were previously selected by the user.

However, if I replace my hardcoded form tags with the cgi functions start_form and end_form, then your problem appears.

#!/usr/bin/perl -wT use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use strict; use warnings; my $q = CGI->new; print $q->header(); print <<"END_HTML"; <html> <body> END_HTML print $q->start_form() . "\n"; # <------ Changed code foreach my $f (qw(foo bar baz)) { print "<p>$f</p>\n"; print $q->radio_group( -name => $f, -values => [qw(c r t s)], -default => '-', ) . "\n"; } print $q->submit(-name => 'submit', -value => 'save') . "\n"; print $q->end_form() . "\n"; # <------ Changed code print <<"END_HTML"; </body> </html> END_HTML 1; __END__

I therefore suggest that you start your investigation there. At the very least you can hardcode your form tags and avoid the problem, but it appears the end_form function adds the following text that for some reason creates your issue.

<div><input type="hidden" name=".cgifields" value="bar" /><input type +="hidden" name=".cgifields" value="baz" /><input type="hidden" name=".cgifields +" value="f oo" /></div>
- Miller

Replies are listed 'Best First'.
Re^2: How to get unselected radio_group() items after submitting form
by slugger415 (Monk) on Mar 08, 2011 at 21:50 UTC
    Interesting! Yes, Miller, that worked, much as I hate hard coding... :-)

    Thanks also to Wallisds for a more official solution.

    Scott

      If you look at the source code of CGI, you can discover the code inside end_form that is adding all the extra hidden variables.

      sub end_form { my($self,@p) = self_or_default(@_); if ( $NOSTICKY ) { return wantarray ? ("</form>") : "\n</form>"; } else { if (my @fields = $self->get_fields) { return wantarray ? ("<div>",@fields,"</div>","</form>") : "<div>".(join '',@fields)."</div>\n</fo +rm>"; } else { return "</form>"; } } }

      As you can see above, the $NOSTICKY global will prevent the extra hidden fields that lead to the undesired default values. After reading the documentation of $NOSTICKY, you can set this in two different ways. One of them is more global:

      use CGI qw(-nosticky);

      Or you can is isolate the change to just the end_form function by doing the following:

      print do {local $CGI::NOSTICKY = 1; $q->end_form()} . "\n";

      Or you can just simplify the whole process for yourself by writing a simple </form> tag. Really don't know why someone wouldn't just do that anyway as the CGI module really does try to do too much in my opinion.

      - Miller