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

Hello Monks,

This question is about incorporating java script(?) in a perl + CGI program.
Am not sure whether java script is the one to be used or can be done in perl-CGI itself

This is the problem:
I have a set of text area fields ( eg: 3 text area displayed in a row )
and when the user clicks a button, I would like to add another set of text area fields
eventually,
2 set of textarea in different lines


I tried the following
add_set_of_text_area; if ( $ENV{'QUERY_STRING'} =~ /^Button_name/ ) { add_set_of_text_area; # this displays 3 text area fields }
The problem with this as you can see is the maximum set of textarea fields that can be generated is only 2 sets.
As and when the user clicks the button, I need to keep generating the set of textarea fields.
How can I keep track of the set of text area fields already generated?

Can you please provide me some pointers on how to keep generating the set of textarea fields on clicking the 'button'?

Replies are listed 'Best First'.
Re: perl + cgi - onclick generation problem
by almut (Canon) on Dec 11, 2008 at 06:08 UTC
    How can I keep track of the set of text area fields already generated?

    You could store the number of sets you already have on a certain page in a hidden field, and then always generate one more (n+1) ...

    You only need Javascript to do this if you want to avoid the round trip to the server for every addition of a new set of text fields.

    if ( $ENV{'QUERY_STRING'} =~ /^Button_name/ ) {

    It's most likely easier to use a module such as CGI for extracting parameters, rather than matching stuff in QUERY_STRING yourself.  As a side effect, it would also provide features such as sticky form values, which might come in handy in this case (if the user has already entered stuff in the existing fields).

      Thanks for the reply

      Sorry, am not clear with how to do that.
      I can retrieve the value in the hidden field using
      $cgi->param('hidden_field');
      But how to set the incremented value in the same hidden field ( this value indicates the number of set of textarea fields displayed )
      The only way I know to set a value for hidden_field is while creating, am not sure how to do the same without creating a new hidden_field for each time

      Any pointers?
        But how to set the incremented value in the same hidden field (...) The only way I know to set a value for hidden_field is while creating

        Well, you are creating the field anew for every page view, anyway (if we're talking about the non-javascript variant, that is), so just set the value as you did the first time...

        Or, in case you're using CGI for HTML generation, too (not just for parameter parsing), you can modify the (otherwise sticky) value with:

        $cgi->param('hidden_field', 'new value');
Re: perl + cgi - onclick generation problem
by parv (Parson) on Dec 11, 2008 at 06:13 UTC

    (Note that there are more than one kind of Java script, not just in terms of version numbers.)

    There is nothing, in what you have shown, stopping you from adding infinite number of widgets (unless the user's client dies). You would just keep the action attribute (of the form) set to the Perl program which outputs additional set of widgets. Am I missing something?

    Of course, you could write the addition of widgets in JavaScript as you originally mentioned. Same difference, other than a CGI program would have to maintain state of all the previous widgets resulting in a seemingly useless trip to the server. OTOH, JavaScript will not work where it does not, obviously.