accessing numeric variables from textfield input in a form

R

rallabs

Dear Perl Experts: A weird thing is happening to me when I try to use
numeric input from a textfield form. The following two scripts, named
'in_sides.cgi' and 'findhyp.cgi', illustrate the problem.
'findhyp' is supposed to do some math on the two input variables
from 'in_sides.cgi', and it does it successfully, but when I try to
store the two input variables into an array (line 25 of the script
'findhyp'), the resulting array has only one element says the
script, and it gives a nonsensical foreach result. In addition, two
messages appear in the error log, saying: "$side[0] not numeric in line
25" and "$side[1] not numeric in line 25". I only get these complaints
when I try to store the variables into the array, but not otherwise. I
have seen in several references, eg "Perl and CGI for the WWW" by
Elizabeth Castro, that string variables automatically convert to
numeric when used in an arithmetic expression, and so I tried on line
24 of the second script to add zero to each variable, but it didn't
work. The error message for line 25 refers to the variables as
'$side[0]' or '$side[1]' even though they are now called
'$pct'. If anybody can explain this I'll be forever grateful.
Thanks in advance.
Mike
The first script ('in_sides.cgi') is:
#!/usr/bin/perl -wT
#this is in_sides.cgi
use CGI::Carp qw(fatalsToBrowser);
use CGI qw:)standard);
use strict;
use diagnostics;
use URI::Escape;
$CGI::DISABLE_UPLOADS=1;
$CGI::pOST_MAX=500;
my $co = new CGI;
print $co->header("text/html");
print $co->start_html;
print $co->center($co->h1("This is where we input the two sides to the
right triangle\n"));
my @sidesarray;
$sidesarray[0]='a:';
$sidesarray[1]='b:';
print $co->startform(-method=>"POST",-action=>"findhyp.cgi");
foreach (0..$#sidesarray){
print $co->h3($sidesarray[$_]," ",textfield("side[$_]"));
}
print $co->p,$co->submit,$co->reset,
$co->endform;
exit;
and the second script ('findhyp.cgi') is:
#!/usr/bin/perl -wT
#This is findhyp.cgi
use strict;
use warnings;
use CGI qw:)standard);
$CGI::DISABLE_UPLOADS=1;
$CGI::pOST_MAX = 102_400; #100KB
my $cc = new CGI;
print $cc->header;
print $cc->start_html(-title=>'findhyp');
my @percentS;
my $sumsq=0.0;
if (param()){
my @param=param();#these are the two numbers from textboxes on previous
page
foreach my $s (param () ) {
my $ss=param($s);
print $cc->h2("The variable \$s is $ss\n");
$sumsq=$sumsq + param($s)*param($s); #sums up squares of two sides
}
my $squirt=sqrt($sumsq);
print $cc->h2("The variable \$sumsq is $sumsq\n");
print $cc->h2("The variable \$squirt is $squirt\n");
foreach my $s ( param() ) {
my $pcnt=0+param($s);# this is supposed to make the $pcnt variable
numeric according to Castro p24
$percentS[$s] =$pcnt; #we're getting 2 messages in the errorlog saying
"$side[n] not numeric in line 25"; one for each pass thru the foreach
}
}
my $numelements=scalar(@percentS);
print $cc->h2("The number of elements in array \@percentS is
$numelements\n");
print $cc->h2("The index number of the last element is $#percentS\n");
exit;
 
A

attn.steven.kuo

(e-mail address removed) wrote:

(snipped)
two
messages appear in the error log, saying: "$side[0] not numeric in line
25" and "$side[1] not numeric in line 25".
(snipped)


and the second script ('findhyp.cgi') is:
#!/usr/bin/perl -wT
#This is findhyp.cgi
use strict;
use warnings;
use CGI qw:)standard);
$CGI::DISABLE_UPLOADS=1;
$CGI::pOST_MAX = 102_400; #100KB
my $cc = new CGI;
print $cc->header;
print $cc->start_html(-title=>'findhyp');
my @percentS;
my $sumsq=0.0;
if (param()){
my @param=param();#these are the two numbers from textboxes on previous
page
foreach my $s (param () ) {
my $ss=param($s);
print $cc->h2("The variable \$s is $ss\n");
$sumsq=$sumsq + param($s)*param($s); #sums up squares of two sides
}
my $squirt=sqrt($sumsq);
print $cc->h2("The variable \$sumsq is $sumsq\n");
print $cc->h2("The variable \$squirt is $squirt\n");
foreach my $s ( param() ) {
my $pcnt=0+param($s);# this is supposed to make the $pcnt variable
numeric according to Castro p24
$percentS[$s] =$pcnt; #we're getting 2 messages in the errorlog saying
"$side[n] not numeric in line 25"; one for each pass thru the foreach


You need an integer index to access a particular
element of the array @percentS. The value in
your $s variable is not an integer.

Was param($s) the integer you wanted?
If so, try:

$percentS[ param($s) ] = $pcnt;

instead of

$percentS[ $s ] = $pcnt;
 
R

rallabs

(e-mail address removed) wrote:

Was param($s) the integer you wanted?

--
Hope this helps,
Steven
Steven, Thanks very much, it DID help. Your insight helped me analyze
the situation in a more logical and sane way. I now realize that the
root of my problem is the necessity to use param() without any
arguments. This is because I haven't any idea how many textfields will
be presented for parsing. (my post contained a form with 2 textfields
only, but that's an oversimplification of the real situation, in which
the form presents as little as 1 and as many as 9 textfields.) When
one uses param with no arguments, one gets the NAMES of the textfields,
not their contents. The problem is how to get the contents. The
following is what I tried. Basically, I filled an array with the names
of the textfields and then tried to use param on each array element in
turn. I don't understand why it doesn't work, but I at least
understand why I was getting the error log msg. Here's the guts of my
script 'parse.cgi':

if (param()){
my @Parray=param();#these are the contents of textboxes in submitting
form, 1-9 of them
my $lenparr=scalar(@Parray);
print $cc->h2("The number of elements in \@Parray is $lenparr. \n");
foreach (0..$#Parray) {
print $cc->h3("\$Parray[$_] is $Parray[$_]. Content of checkbox#$_ is
param($Parray[$_]).\n");
}
The last operation, param($Parray[$_]) , does not yield the contents of
$Parray[$_], but rather the words " param(side[N] " for pass "N" thru
the foreach. Yukk. Thank you very much for the help.
Mike
(e-mail address removed) wrote:

(snipped)
two
messages appear in the error log, saying: "$side[0] not numeric in line
25" and "$side[1] not numeric in line 25".
(snipped)


and the second script ('findhyp.cgi') is:
#!/usr/bin/perl -wT
#This is findhyp.cgi
use strict;
use warnings;
use CGI qw:)standard);
$CGI::DISABLE_UPLOADS=1;
$CGI::pOST_MAX = 102_400; #100KB
my $cc = new CGI;
print $cc->header;
print $cc->start_html(-title=>'findhyp');
my @percentS;
my $sumsq=0.0;
if (param()){
my @param=param();#these are the two numbers from textboxes on previous
page
foreach my $s (param () ) {
my $ss=param($s);
print $cc->h2("The variable \$s is $ss\n");
$sumsq=$sumsq + param($s)*param($s); #sums up squares of two sides
}
my $squirt=sqrt($sumsq);
print $cc->h2("The variable \$sumsq is $sumsq\n");
print $cc->h2("The variable \$squirt is $squirt\n");
foreach my $s ( param() ) {
my $pcnt=0+param($s);# this is supposed to make the $pcnt variable
numeric according to Castro p24
$percentS[$s] =$pcnt; #we're getting 2 messages in the errorlog saying
"$side[n] not numeric in line 25"; one for each pass thru the foreach


You need an integer index to access a particular
element of the array @percentS. The value in
your $s variable is not an integer.

Was param($s) the integer you wanted?
If so, try:

$percentS[ param($s) ] = $pcnt;

instead of

$percentS[ $s ] = $pcnt;
 
A

Amer Neely

(e-mail address removed) wrote:

Was param($s) the integer you wanted?

--
Hope this helps,
Steven
Steven, Thanks very much, it DID help. Your insight helped me analyze
the situation in a more logical and sane way. I now realize that the
root of my problem is the necessity to use param() without any
arguments. This is because I haven't any idea how many textfields will
be presented for parsing. (my post contained a form with 2 textfields
only, but that's an oversimplification of the real situation, in which
the form presents as little as 1 and as many as 9 textfields.) When
one uses param with no arguments, one gets the NAMES of the textfields,
not their contents. The problem is how to get the contents. The
following is what I tried. Basically, I filled an array with the names
of the textfields and then tried to use param on each array element in
turn. I don't understand why it doesn't work, but I at least
understand why I was getting the error log msg. Here's the guts of my
script 'parse.cgi':

if (param()){
my @Parray=param();#these are the contents of textboxes in submitting
form, 1-9 of them
my $lenparr=scalar(@Parray);
print $cc->h2("The number of elements in \@Parray is $lenparr. \n");
foreach (0..$#Parray) {
print $cc->h3("\$Parray[$_] is $Parray[$_]. Content of checkbox#$_ is
param($Parray[$_]).\n");
}
The last operation, param($Parray[$_]) , does not yield the contents of
$Parray[$_], but rather the words " param(side[N] " for pass "N" thru
the foreach. Yukk. Thank you very much for the help.
Mike
(e-mail address removed) wrote:

(snipped)
two
messages appear in the error log, saying: "$side[0] not numeric in line
25" and "$side[1] not numeric in line 25". (snipped)


and the second script ('findhyp.cgi') is:
#!/usr/bin/perl -wT
#This is findhyp.cgi
use strict;
use warnings;
use CGI qw:)standard);
$CGI::DISABLE_UPLOADS=1;
$CGI::pOST_MAX = 102_400; #100KB
my $cc = new CGI;
print $cc->header;
print $cc->start_html(-title=>'findhyp');
my @percentS;
my $sumsq=0.0;
if (param()){
my @param=param();#these are the two numbers from textboxes on previous
page
foreach my $s (param () ) {
my $ss=param($s);
print $cc->h2("The variable \$s is $ss\n");
$sumsq=$sumsq + param($s)*param($s); #sums up squares of two sides
}
my $squirt=sqrt($sumsq);
print $cc->h2("The variable \$sumsq is $sumsq\n");
print $cc->h2("The variable \$squirt is $squirt\n");
foreach my $s ( param() ) {
my $pcnt=0+param($s);# this is supposed to make the $pcnt variable
numeric according to Castro p24
$percentS[$s] =$pcnt; #we're getting 2 messages in the errorlog saying
"$side[n] not numeric in line 25"; one for each pass thru the foreach

You need an integer index to access a particular
element of the array @percentS. The value in
your $s variable is not an integer.

Was param($s) the integer you wanted?
If so, try:

$percentS[ param($s) ] = $pcnt;

instead of

$percentS[ $s ] = $pcnt;

According to 'Official Guide to Programming with CGI.pm' [Lincoln Stein]
this should walk through all the form fields:

foreach $name(param())
{
$value=param($name);
print "The value of $name is $value\n";
}

--
Amer Neely
w: www.softouch.on.ca/
b: www.softouch.on.ca/blog/
Perl | MySQL programming for all data entry forms.
"We make web sites work!"
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top