Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

multiple values and CGI.pm

by Kolyan (Beadle)
on Mar 22, 2001 at 15:58 UTC ( [id://66308]=perlquestion: print w/replies, xml ) Need Help??

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

I know that CGI.pm is useful than someone wants to handle multiple values of one parameter. So I tried the following:
#!/usr/bin/perl -w use CGI qw/:standard/; print header, start_html, start_form, textfield('name'), textfield('na +me'), submit, end_form; if (param) { my @values = param('name'); print "first: $values[0]", br, "second: $values[1]"; } print end_html;
But with this example I wasn't able to get both previous values on resulting page. Both textfields contain first value. So, can anyone explain what happens here? Is there a workaround?

Replies are listed 'Best First'.
Re: multiple values and CGI.pm
by Trimbach (Curate) on Mar 22, 2001 at 17:54 UTC
    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.

      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")
Re: multiple values and CGI.pm
by modred (Pilgrim) on Mar 22, 2001 at 17:58 UTC
    The problem you are having may be with the call to textfield('name').

    When you use CGI.pm's functions to print out form elements they exhibit a certain stickiness. That is, if you already have elements that have been passed in as parameters to the script then CGI.pm will, by default, print out the passed in value. If you want to give it another value than the one that was originally passed in, you need to either pass it an explicit value and the -override=>1 argument or use something like $query->param('foo',"I'm taking over this value!");.

    Both of the above methods are described under the section "Creating a Text Field" in the text obtained by doing a perldoc CGI.

    So, in your above example, you are making two calls to textfield('name'). Textfield appears to only take a scalar argument, so only the first element of the array is used in both cases. CGI.pm does not remember if it has printed the element before or not.

Re: multiple values and CGI.pm
by davorg (Chancellor) on Mar 22, 2001 at 16:09 UTC

    Looks to me like the code should work fine. How are you testing it? What results are you getting?

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      I wasn't clear enough in my question.
      When (for example) I put 1 into first textfield and 2 into second I get the following html as a result:
      <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
      <HTML>
      <HEAD><TITLE>Untitled Document</TITLE></HEAD>
      <BODY>
      <FORM METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
      --------------------------------
      The problem is on the next line:
      --------------------------------
      <INPUT TYPE="text" NAME="name" VALUE="1"><INPUT TYPE="text" NAME="name" VALUE="1">
      <INPUT TYPE="submit" NAME=".submit"></FORM>
      --------------------------------
      The rest work as I expected:
      --------------------------------
      first: 1
      second: 2
      </BODY>
      </HTML>
      Note that VALUE="1" attributes in textfields.
      Both corresponds to first value.
      And what about second? Can I get it in place?
      Is that a feature of CGI.pm or I missed something?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://66308]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found