Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

problem with form value (using cgi.pm)

by MCS (Monk)
on Feb 16, 2004 at 04:26 UTC ( #329215=perlquestion: print w/ replies, xml ) Need Help??
MCS has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a CGI front end to a database for recipes... I have a lot of it working but I am having trouble with my edit recipe form. The bit that is causing me problems is this:

for (my $i=0; $i<$numberofingredients; $i++) { print "$ingredientAoH[$i]{qty}\n"; print "Qty:", $query->textfield(-name=>"qty$ingredientAoH[$i]{ +ingID}", -default=>"$ingredientAoH[$i]{ +qty}" -size=>'10', -maxlength=>'20', "\n"); print "Amt:", $query->textfield(-name=> "ammount$ingredientAoH +[$i]{ingID}", -default=> "$ingredientAoH[$i] +{amt}", -size=>10, -maxlength=>100, "\n"); print "Des:", $query->textfield(-name=> "description$ingredien +tAoH[$i]{ingID}", -default=> "$ingredientAoH[$i] +{des}", -size=>50, -maxlength=>100), "\n"; print $query->hidden(-name=>"ingID$ingredientAoH[$i]{ingID}", +-default=>"$ingredientAoH[$i]{ingID}"), "<br>\n"; }

Now I put that extra print in there at the top for debugging purposes and it does in fact print out the correct value. However, the actual text field just displays the first whole number. So if I have a value of say 1/4, it just shows up as 1, 1 1/2 shows up as 1, etc... Larger numbers work (ie. 12 shows up as 12) but it doesn't seem to like spaces or slashes. Does anyone know why and what I could do to fix it?

Oh and as an aside, if I type in the correct value and submit it, the correct values go into the database. It just doesn't read them properly as the default values.

Comment on problem with form value (using cgi.pm)
Download Code
Re: problem with form value (using cgi.pm)
by Zaxo (Archbishop) on Feb 16, 2004 at 04:43 UTC

    You've shown how you print the form, but not what you do with the values you get. Somewhere, Perl is converting the strings to numbers. '/' is not a part of a number string, so that is where Perl quits trying.

    It is perilous, but you could eval the string to get the division done. Perilous in case some rascal gets you to eval "system '/bin/rm', '-rf', $ENV{HOME};". You could also look at Math::Expr, which is perhaps safer.

    After Compline,
    Zaxo

      I've shown how I print the values because that's where I'm having the problems. If I delete the default and type in the correct value (say "1 1/2") It works fine. However, when it prints the form, instead of printing the actual value of $ingredientAoH[$i]{qty}, it just prints the first number before a space or a slash. I've verified that $ingredientAoH[$i]{qty} contains the correct value because of the extra print statement in there.

      Oh and I don't want to evaluate the expression because I most recipes have things like 1/2 tsp salt... not 0.5 tsp salt. The only thing I can think of is that "-default=>"$ingredientAoH[$i]{qty}"" converts the string into a number. (which I don't want to do)

Re: problem with form value (using cgi.pm)
by Trimbach (Curate) on Feb 16, 2004 at 05:40 UTC

      Nope... didn't help :-(

        Ok. Here's your problem: you're missing a comma in your print statement. It should read like so:
        print "Qty:", $query->textfield(-name=>"qty$ingredientAoH[$i]{ingID}", -default=>"$ingredientAoH[$i]{qty}", -size=>'10', -maxlength=>'20');

        Note the comma between -default=>"$ingredientAoH[$i]{qty}" and -size=>'10'. That fixes your problem. :-)

        Gary Blackburn
        Trained Killer

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-12-21 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls