0

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...

2
  • Does the generated HTML contain the expected value? Commented Aug 30, 2011 at 20:10
  • What are the values for SEND_OPTIONS_CONCAT_1 etc...? Commented Aug 30, 2011 at 20:11

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.