开发者

Perl variable not displaying correct value

开发者 https://www.devze.com 2023-04-01 20:44 出处:网络
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 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.开发者_开发技巧..


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.


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
        ),
0

精彩评论

暂无评论...
验证码 换一张
取 消