arrays of parameters in CGI.pm

Discussion in 'Perl Misc' started by Steve (another one), Apr 7, 2004.

  1. Dear all

    I have a script which writes an html form using CGI.pm. It is very
    convenient to have multiple fields with the same name from which I can
    extract values by treating them as an array. The problem is that on
    subsequent submissions of the form, all values default to the first. The
    only way I can reset them to the value that they previously had is to
    use javascript to assign their values to those found in the parameters
    array. This is very ugly, there must be a better way, does anyone have
    any suggestions ?

    Thanks

    Below is a minimal example, enter different values in the boxes and
    submit - values are sucessfully recovered but defaults revert to the
    value on box 0.

    #!/usr/bin/perl

    use strict;
    use warnings;
    use CGI;

    my $q = new CGI;

    my @param_names = $q->param;
    my %myparams;

    foreach (@param_names){
    (@{$myparams{$_}}=$q->param($_))=~ s/[^a..z,A..Z]/_/g;
    }

    print $q->header(-type =>"text/html", -expires=>"-1d"),
    $q->start_html(),
    $q->start_form(-method=>'post');

    for my $cnt (0..5) {print "box $cnt ",
    $q->textfield(-name=>'text'),'<br>'}

    print '<br><br>',
    $q->submit(-name=>'submitbutton', -value=>'Submit'),
    $q->endform;

    foreach (@{$myparams{text}}) {print "<br>$_\n"}

    print $q->end_html;
     
    Steve (another one), Apr 7, 2004
    #1
    1. Advertising

  2. Steve (another one)

    Paul Lalli Guest

    On Wed, 7 Apr 2004, Steve (another one) wrote:

    > Dear all
    >
    > I have a script which writes an html form using CGI.pm. It is very
    > convenient to have multiple fields with the same name from which I can
    > extract values by treating them as an array. The problem is that on
    > subsequent submissions of the form, all values default to the first. The
    > only way I can reset them to the value that they previously had is to
    > use javascript to assign their values to those found in the parameters
    > array. This is very ugly, there must be a better way, does anyone have
    > any suggestions ?
    >
    > Thanks
    >
    > Below is a minimal example, enter different values in the boxes and
    > submit - values are sucessfully recovered but defaults revert to the
    > value on box 0.
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    > use CGI;
    >
    > my $q = new CGI;
    >
    > my @param_names = $q->param;
    > my %myparams;
    >
    > foreach (@param_names){
    > (@{$myparams{$_}}=$q->param($_))=~ s/[^a..z,A..Z]/_/g;
    > }
    >
    > print $q->header(-type =>"text/html", -expires=>"-1d"),
    > $q->start_html(),
    > $q->start_form(-method=>'post');
    >
    > for my $cnt (0..5) {print "box $cnt ",
    > $q->textfield(-name=>'text'),'<br>'}


    $q->textfield(-name=>'text', -override=>1, -value=>$myparams{text}[$cnt])

    Because all the text fields are named the same, CGI.pm doesn't know which
    value from the parameter list you want to go into any particular field
    with that name. The above code will force the value of the boxes to be
    equal to the correct position in the @{myparams{text}} array. The
    -override parameter is necessary to force the text boxes to use the values
    you've assigned, rather than the defaults.

    >
    > print '<br><br>',
    > $q->submit(-name=>'submitbutton', -value=>'Submit'),
    > $q->endform;
    >
    > foreach (@{$myparams{text}}) {print "<br>$_\n"}
    >
    > print $q->end_html;



    Paul Lalli>
     
    Paul Lalli, Apr 7, 2004
    #2
    1. Advertising

  3. >
    > $q->textfield(-name=>'text', -override=>1, -value=>$myparams{text}[$cnt])
    >


    Thanks
     
    Steve (another one), Apr 7, 2004
    #3
  4. Steve (another one)

    Dave Weaver Guest

    On Wed, 07 Apr 2004 13:04:56 +0100, Steve (another one)
    <> wrote:

    > (@{$myparams{$_}}=$q->param($_))=~ s/[^a..z,A..Z]/_/g;


    ^^^^^^^^^^^^
    I suspect that doesn't do what you expected.
    You want to replace anything that's not 'a' '.' 'z' ',' 'A' or 'Z' with
    an underscore?

    You possibly meant: s/[^a-zA-Z]/_/g;
    or better: tr/a-zA-Z/_/c;

    Run "perldoc perlop" and "perldoc perlre" for more info.

    --
    Dave.
     
    Dave Weaver, Apr 8, 2004
    #4
  5. Dave Weaver wrote:
    > On Wed, 07 Apr 2004 13:04:56 +0100, Steve (another one)
    > <> wrote:
    >
    >
    >> (@{$myparams{$_}}=$q->param($_))=~ s/[^a..z,A..Z]/_/g;

    >
    >
    > ^^^^^^^^^^^^
    > I suspect that doesn't do what you expected.
    > You want to replace anything that's not 'a' '.' 'z' ',' 'A' or 'Z' with
    > an underscore?
    >
    > You possibly meant: s/[^a-zA-Z]/_/g;
    > or better: tr/a-zA-Z/_/c;
    >
    > Run "perldoc perlop" and "perldoc perlre" for more info.
    >

    Urrm yes you are right. I wrecked this while hacking it down from a more
    complex regex to illustrate what was happening to my params as they came
    in. However as you point out it shouldn't let anything but those chars
    through, but it does so I am obviously doing the assignment wrong. Any
    thoughts ?

    my @param_names = $q->param;
    my %myparams;
    foreach (@param_names){
    (@{$myparams{$_}}=$q->param($_))=~ s/[^A-Za-z]/_/g;
    # this is not doing what I expect,
    # but now I can't see why it should !
    }
     
    Steve (another one), Apr 8, 2004
    #5
  6. Steve (another one)

    Paul Lalli Guest

    On Thu, 8 Apr 2004, Steve (another one) wrote:

    > Dave Weaver wrote:
    > > On Wed, 07 Apr 2004 13:04:56 +0100, Steve (another one)
    > > <> wrote:
    > >
    > >
    > >> (@{$myparams{$_}}=$q->param($_))=~ s/[^a..z,A..Z]/_/g;

    > >
    > >
    > > ^^^^^^^^^^^^
    > > I suspect that doesn't do what you expected.
    > > You want to replace anything that's not 'a' '.' 'z' ',' 'A' or 'Z' with
    > > an underscore?
    > >
    > > You possibly meant: s/[^a-zA-Z]/_/g;
    > > or better: tr/a-zA-Z/_/c;
    > >
    > > Run "perldoc perlop" and "perldoc perlre" for more info.
    > >

    > Urrm yes you are right. I wrecked this while hacking it down from a more
    > complex regex to illustrate what was happening to my params as they came
    > in. However as you point out it shouldn't let anything but those chars
    > through, but it does so I am obviously doing the assignment wrong. Any
    > thoughts ?
    >
    > my @param_names = $q->param;
    > my %myparams;
    > foreach (@param_names){
    > (@{$myparams{$_}}=$q->param($_))=~ s/[^A-Za-z]/_/g;
    > # this is not doing what I expect,
    > # but now I can't see why it should !


    the =~ operator takes a scalar on the left, not an array or list. Don't
    try to combine these two steps.

    foreach (@param_names) {
    @{$myparams{$_}} = $q->param($_);
    tr/a-zA-Z/_/c for @{$myparams{$_}};
    }

    Paul Lalli
     
    Paul Lalli, Apr 8, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    997
    Jonck van der Kogel
    May 27, 2004
  2. Jason
    Replies:
    2
    Views:
    525
    Jonathan Mcdougall
    May 13, 2006
  3. Philipp
    Replies:
    21
    Views:
    1,156
    Philipp
    Jan 20, 2009
  4. kito
    Replies:
    2
    Views:
    425
  5. Florian Loitsch
    Replies:
    11
    Views:
    254
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page