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

To a large extent, this is a follow-up to CGI: which field has changed?. I am trying to identify whether fields have changed. I have three menus, and am having no problem with them. But the text area is causing me problems. If I point my browser at the following code, the "Old" that appears above the text area is permanently zero length. At the end of the form, it is reported to contain text. This can be changed by editing the text area, unsurprisingly. But the value at the start of the code remains resolutely blank. I have tried using value instead of default, but the problem remains. I find it very strange that I should not get the same problem with the popup menus. I am running Losedows XP Pro, Apache 2.2.16, ActiveState 5.12.2, CGI 3.52 and FireFox 3.6.15.

Regards,

John Davies

use strict; use warnings; use lib qw(. lib); use CGI; use CGI qw/:standard -debug/; print_form(); sub print_form { print header; print start_html("Problem demo"); print "<h1>Problem demo</h1>\n"; print start_form; print "Old: " . param('OldBugText') . "<br>"; text_field(); print submit('Action', 'Submit'); print "</p>"; set_old_params(); show_vars(); print end_form; print "<hr>\n"; print end_html; } sub text_field { my $bugtext = "Example Text"; print "<p>", textarea(-name => 'BugText', -value => $bugtext, -rows => 10, -columns => 40); } sub show_vars { print "<h2>Here are the current settings in this form</h2>"; for my $key (param) { print "<strong>$key</strong> -> "; my @values = param($key); print join(", ",@values),"<br>\n"; } } sub set_old_params { for('BugText') { if (defined param($_)) { param(-name => "Old" . $_ , -value => param($_)); } else { param(-name => "Old" . $_ , -value => ''); } } }

Replies are listed 'Best First'.
Re: CGI: retaining textarea contents in hidden field
by Eliya (Vicar) on Mar 09, 2011 at 22:16 UTC
    the "Old" that appears above the text area is permanently zero length

    Your form doesn't have a field/widget named "OldBugText" (neither hidden nor otherwise), and setting param(-name => "OldBugText", ...) doesn't magically create one.  As a result, the browser doesn't send a key-value pair for OldBugText to the CGI script (because it never got it itself as part of the form).  So when you try to print "Old: " . param('OldBugText'), the parameter is empty.

    See Creating a Hidden Field.

      Sigh. I hate showing how stupid I can be. But you've saved me a lot of time. Many thanks.

      Regards,

      John Davies

Re: CGI: retaining textarea contents in hidden field
by wind (Priest) on Mar 10, 2011 at 02:32 UTC

    As Eliya already stated, you weren't accurately creating a hidden field.

    It's also important to note that if you want to capture the previous value of OldBugText, then you're going to have to save it in a temporary variable since you want to overwrite it each time as well. Something like the following is probably what you're aiming for:

    #!/usr/bin/perl -wT use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use strict; use warnings; my $q = CGI->new; my $prev = $q->param('OldBugText'); $prev = '' if ! defined $prev; $q->param('OldBugText' => $q->param('BugText')); print $q->header; print $q->start_html("Problem demo"); print "<h1>Problem demo</h1>\n"; print $q->start_form; print $q->hidden(-name => "OldBugText"); print "<p>Previous Value: ", $q->escapeHTML($prev), "</p>"; print "<p>", $q->textarea( -name => 'BugText', -value => "Example Text", -rows => 10, -columns => 40 ), "</p>"; print "<p>", $q->submit('Action', 'Submit'), "</p>"; print $q->end_form; print "<hr>\n"; print $q->end_html; 1; __END__