Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

perl CGI form ignores value

by anothersmurf (Novice)
on Sep 17, 2012 at 00:20 UTC ( #993944=perlquestion: print w/ replies, xml ) Need Help??
anothersmurf has asked for the wisdom of the Perl Monks concerning the following question:

I have some code where default values for textfields() are supplied from a database.

I have occasion to call the app with a different value, but I want the different value to be ignored and the default value from the database to be used.

I've tried setting it with -override in the textfield() as well as calling param() to set it specifically, but the output on the webpage is always the POST-supplied information instead of the -value=>"db data".

Any idea on how I can make it listen to me?

For now I have forced a redirect so it pulls data from the database (as there is no supplied data). It works fine, but it seems like a hack. It seems like there should be a way for the form value to be programmed, ignoring the POST parameter.

Comment on perl CGI form ignores value
Re: perl CGI form ignores value
by Anonymous Monk on Sep 17, 2012 at 02:12 UTC

    It seems like there should be a way for the form value to be programmed, ignoring the POST parameter.

    You mean, like tell CGI.pm not to read any vars?

    Sure  CGI->new({}); It will still read cookies as those come via %ENV

Re: perl CGI form ignores value
by rjt (Deacon) on Sep 17, 2012 at 03:10 UTC

    There are several ways you can do this, but the following is reasonably efficient, and hopefully your parameter list is relatively small anyway:

    my $q = new CGI; my %vars = $q->Vars; # Fetch all CGI parameters my %db_vars = pop_defaults(); # Fetch from database # Override defined values from database $vars{$_} = $db_vars{$_} for (keys %db_vars); # Fetch all defaults from the DB and return as a hash ref sub pop_defaults { my $r = { }; ... return $r }

    You can also simply loop through the params:

    $vars{$_} = $db_vars{$_} // $q->param($_) for ($q->param);

    Finally, if you prefer to merge hash slices:

    %vars{keys %db_vars} = values %db_vars;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-08-02 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls