skeniver
skeniver

Reputation: 2647

Perl variable not displaying correct value

I will attempt this question again, as apparently the last time I asked it, I didn't do it very well... Here goes again:

I have this bit of code, which take parameters from a web form and depending on the input parameter should display text in a textarea.

The if statement that sets the $defMessage variable is running properly, but no matter what the input variables value is, the default text in the textarea doesn't change to the actual value stored in $defMessage.

Can anybody spot why this might be happening?

my $defMessage = undef;

$defMessage = 'CONCAT 1';

if ($templateLength =~ SEND_OPTIONS_CONCAT_1) {
    $defMessage = 'CONCAT 1';
} elsif ($templateLength =~ SEND_OPTIONS_CONCAT_2) {
    $defMessage = 'CONCAT 2';
} elsif ($templateLength =~ SEND_OPTIONS_CONCAT_3) {
    $defMessage = 'CONCAT 3';
}

print $q->start_form(
    -name=>'main',
    -method=>'POST',
);

print $q->start_table(
    {-align=>'center', -border=>1}
);
print $q->Tr(
    $q->td(
        {-align=>'center'},
        'Message<br>'.$q->textarea(
            -name=>'sendMessage',
            -size=>15,
            -rows=>10,
            -columns=>15,
            -value=>$defMessage,
        ),
    ),
);

I have tried changing

my $defMessage = undef;

to

use vars qw($defMessage);

but that didn't work either...

Upvotes: 0

Views: 237

Answers (2)

ysth
ysth

Reputation: 98388

If the request you are processing provides a field_name parameter, CGI will use that value instead of the default value you supply unless you either call textarea with -override=>1 or you explicitly change the parameter ($q->param('field_name',$defMessage)) before calling textarea.

This isn't specific to textarea; all CGI's form input methods work this way.

Upvotes: 4

Stuart Watt
Stuart Watt

Reputation: 5391

Text area items are not like other controls in HTML, as the value attribute isn't used. Instead, the contents of the item is what matters. This shows up in a slightly different interface. The CGI documentation (see: http://search.cpan.org/dist/CGI/lib/CGI.pm#CREATING_A_BIG_TEXT_FIELD) shows the key to use for a default value is -default, not -value.

So, try:

'Message<br>'.$q->textarea(
            -name=>'sendMessage',
#           -size=>15,              # Deleted, doesn't apply to textarea controls
            -rows=>10,
            -columns=>15,
            -default=>$defMessage,  # Amended line
        ),

Upvotes: 0

Related Questions