Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > arrays of parameters in CGI.pm

Reply
Thread Tools

arrays of parameters in CGI.pm

 
 
Steve (another one)
Guest
Posts: n/a
 
      04-07-2004
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;


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-07-2004
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>
 
Reply With Quote
 
 
 
 
Steve (another one)
Guest
Posts: n/a
 
      04-07-2004
>
> $q->textfield(-name=>'text', -override=>1, -value=>$myparams{text}[$cnt])
>


Thanks

 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      04-08-2004
On Wed, 07 Apr 2004 13:04:56 +0100, Steve (another one)
<(E-Mail Removed)> 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.

 
Reply With Quote
 
Steve (another one)
Guest
Posts: n/a
 
      04-08-2004
Dave Weaver wrote:
> On Wed, 07 Apr 2004 13:04:56 +0100, Steve (another one)
> <(E-Mail Removed)> 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 !
}

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      04-08-2004
On Thu, 8 Apr 2004, Steve (another one) wrote:

> Dave Weaver wrote:
> > On Wed, 07 Apr 2004 13:04:56 +0100, Steve (another one)
> > <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
skipping template parameters inside template parameters kito C++ 2 09-26-2010 12:04 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
does a "parameters"-parameter overwrite the "parameters"-object? Florian Loitsch Javascript 11 03-15-2005 03:33 PM
Servlet parameters different from the command line parameters? Jonck van der Kogel Java 2 05-26-2004 11:34 PM



Advertisments