in reply to multiple values and CGI.pm

CGI.pm param data is "sticky" meaning that if you create a field name with the same name as the name of an existing param field the new field will automatically be populated with the "old" param value. (That's the default. CGI.pm provides an "override" option if you don't want that.)

Anyway, your problem is that you're generating a form field BEFORE fetching the param values from the previous invocation of the script. CGI.pm sees a "name" param and has no idea which item in the param('name') list goes in which field on the screen. So it puts the 0 index in both.

In cases like this you need to fetch the param data FIRST, and then set each form fields equal to the correct index of the resulting array, and call the override attribute at the same time. Like this:

#!/usr/bin/perl -w use CGI qw/:standard/; my @values; if (param) { @values = param('name'); } print header, start_html, start_form; print textfield(-name=>'name', -default=>$values[0], -override=>1); print textfield(-name=>'name', -default=>$values[1], -override=>1); print submit, end_form, print end_html;

Gary Blackburn
Trained Killer

Correction: Fixed a scoping problem.

Replies are listed 'Best First'.
Re: Re: multiple values and CGI.pm
by Kolyan (Beadle) on Mar 22, 2001 at 19:17 UTC
    Sorry, but example you provide doesn't work as expected too.
    All I managed to get from it was two empty textfields.
    Also, I was unable to print $values[0], $values[1] like this:
    #!/usr/bin/perl -w use CGI qw/:standard/; if (param) { my @values = param('name'); } print header, start_html, start_form; print textfield(-name=>'name', -default=>$values[0], -override=>1); print textfield(-name=>'name', -default=>$values[1], -override=>1); print submit, end_form, $values[0], $values[1], end_html;
    Have you tried it before?

      Your first problem is that you didn't use strict. Fixing that will probably give you a big clue about your second problem.

      Your "my @values" makes that @values scoped local to the block of the if statement so that your other uses of @values are using a different, undeclared, empty array.

              - tye (but my friends call me "Tye")