arrays of parameters in CGI.pm

  • Thread starter Steve (another one)
  • Start date
S

Steve (another one)

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;
 
P

Paul Lalli

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>
 
D

Dave Weaver

(@{$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.
 
S

Steve (another one)

Dave said:
(@{$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 !
}
 
P

Paul Lalli

Dave said:
(@{$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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top