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

Hello all,

What I want to do is create a form which has three textfields and one "Insert Fields" button. When the button is clicked the three values should be displayed in a textarea below it.
Okay, that bit I can do. The only complication is that after the first click I would like all subsequent clicks to append to the textarea and not replace the current values. I can manage to do it the first time but each subsequent click overwrites the existing textarea.
e.g.
1st time: Fields 1,2,3 = a,b,c (Click Insert) textarea = abc 2nd time: Fields 1,2,3 = x,y,z (Click Insert) textarea = abcxyz
that is what I would like it to do, but what I actually get is textarea = xyz. Here's my code.....
print $query->start_multipart_form("POST", "test"); print "Field Name:"; print $query->textfield(-name=>'FieldOne', -size=>25); print "Field Type:"; print $query->textfield(-name=>'FieldTwo', -size=>25); print "Field Length:"; print $query->textfield(-name=>'FieldThree', -size=>25); print "<br>" . $query->submit('Insert Field'); print $query->endform(); $testText = $query->param('SPCtext') . $query->param('FieldOne') . $qu +ery->param('FieldTwo') . $query->param('FieldThree'); print $query->start_multipart_form("POST", "test"); print $query->textarea(-name=>'SPCtext', -cols=>50, -rows=>10, -value= +>$testText); print $query->submit('Save'); print $query->endform();
I tried having the textarea inside the first form but then the Field values don't exist until after the 'click'.
Am I missing something simple?
Thanks,
AM

Replies are listed 'Best First'.
Re: populating a textarea
by Trimbach (Curate) on Nov 09, 2001 at 17:33 UTC
    ..and the verdict is... both previous posters are correct. There are two problems with your code: 1) You have two form fields, which means that when you press "Insert:" you only get the form fields in form1, when you press "Save" you only get the form fields in form2. Make them one form and you're almost there.

    Problem #2 is that, yes, indeed, CGI is making your data sticky, meaning it will ignore the value you're trying to create for SPCtext because SPCtext already has a value of the empty string. Sometimes this a good thing. In my experience it's a pain in the butt. Fortunately, fixing it is as simple as adding a "force=>1" parameter to your textarea line, like so:

    print $query->textarea(-name=>'SPCtext', -cols=>50, -rows=>10, -value= +>$testText, -force=>1);
    In short, this code is tested and works:
    #!/usr/bin/perl -w use CGI; $query = new CGI; print $query->header; print $query->start_html(); print $query->start_multipart_form("POST", "test.pl"); print "Field Name:"; print $query->textfield(-name=>'FieldOne', -size=>25); print "Field Type:"; print $query->textfield(-name=>'FieldTwo', -size=>25); print "Field Length:"; print $query->textfield(-name=>'FieldThree', -size=>25); print "<br>" . $query->submit('Insert Field'); $testText = $query->param('SPCtext') . $query->param('FieldOne') . $qu +ery->param('FieldTwo') . $query->param('FieldThree'); print $query->textarea(-name=>'SPCtext', -cols=>50, -rows=>10, -value= +>$testText, -force=>1); print $query->submit('Save'); print $query->endform(); print $query->end_html();

    Gary Blackburn
    Trained Killer

      woohoo!!!!!!

      Thanks 'Killer' - Friday 13:40, you've done my work for me, looks like I can go home now.
Re: populating a textarea
by George_Sherston (Vicar) on Nov 09, 2001 at 17:08 UTC
    I think what's happening is that CGI.pm is being very helpful and making your data "sticky" - that's to say, if you send back a form field, it automatically goes back with the same value as it had when the form was submitted. In other words, CGI.pm is ignoring you when you say -value=>$testText.

    The way I have dealt with this (and this may not be the canonical way) is to create the parameter explicitly before calling the textarea method. In other words, insert this line just before you print the textarea field:
    $query->param( -name => 'SPC text', -default => $testText, );
    I haven't tested this particular line in the context of your code, but the same thing works in scripts I have written.

    § George Sherston
Re: populating a textarea
by andye (Curate) on Nov 09, 2001 at 16:54 UTC
    My (untested) theory:

    Having two separate forms means that when the user clicks on the upper submit button, the SPCtext field is not submitted (because it's not part of that form). So  $query->param('SPCtext') is blank.

    Could you expand on I tried having the textarea inside the first form but then the Field values don't exist until after the 'click'. - I'm not with you.

    hth,
    andy.