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.


In reply to Re: multiple values and CGI.pm by Trimbach
in thread multiple values and CGI.pm by Kolyan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.