Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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.

Replies are listed 'Best First'.
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;
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 not to read any vars?

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993944]
Approved by ww
[holli]: *shudder* electronic music? i'm more for the opposite :)
[marto]: good music, is good music, ignore the genre :P
[Corion]: marto: Thank you, reminds me of good times ;))

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-11-18 19:18 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.