markjugg has asked for the wisdom of the Perl Monks concerning the following question:

Hello

Occassionally I run into a problem space where a good solution is to pass information though CGI-based webpages in hidden form fields. (No, not sensitive info. :). I have a feeling that CGI.pm probably has some tricks to help with this, but I can't find them. Here's some code I've used before as my own helper function:

sub export_form_vars { my @input = @_; my $html = "\n"; foreach my $key (@input) { my @vars = param $key; foreach my $val (@vars) { $val = escapeHTML($val); $html .= qq { <input type=hidden name="$key" +value="$val">\n }; } } return $html }
For each of the field names passed in, it produces the appropriate hidden tags, based on the values in param().

Is there an easy way to accomplish the same thing with CGI.pm? Thanks,

-mark

Replies are listed 'Best First'.
Re: passing information through hidden fields using CGI.pm
by damian1301 (Curate) on Feb 13, 2001 at 01:27 UTC
    yay! CGI.pm! :) What you want is:

    print hidden(-name=>'hey',-value=>'you');

    or just
    print hidden('hey,'you');
    Because the first two fields default to name then value. You should check out the documentation for all sorts of amazing stuff it can do.

    Also, never input plain HTML in your script. Usually, thats where all the bugs are because maybe you forgot to backslash a quote:
    print "<input type="text">";
    Would obviously give an error as would many other things. Basically, always use CGI.pm and I am glad that you already are :)

    Almost a Perl hacker.
    Dave AKA damian

    I encourage you to email me
      Since he's just 'passing through' the values, and CGI.pm parameters are 'sticky', this:
      print hidden('hey','you');
      can be shortened to this:
      print hidden('hey');
      In fact, you have to specifically pass an override flag if you want to override the 'sticky' default, so the first example wouldn't even work if you were trying to change the value of the 'hey' parameter.

      Update:damian1301 is wrong below. See this:

      $ perl -e 'use CGI qw(:standard -debug);print hidden("hey")' hey=you <input type="hidden" name="hey" value="you"> $ perl -e 'use CGI qw(:standard -debug); print hidden("hey","there")' hey=you <input type="hidden" name="hey" value="you"> $ perl -e 'use CGI qw(:standard -debug);print hidden(-override=>1, -name=>"hey",-value=>"there")' hey=you <input type="hidden" name="" value="there">
        Very true, thank you. But  print hidden("hey"); would only pass myscript.pl?hey= which might not always be what you wanted. Especially if you want to maintain state in a HTTP authentication through hidden fields. Mostly, it depends on what your doing to decide what you want to use. Thanks. Later

        Almost a Perl hacker.
        Dave AKA damian

        I encourage you to email me
      Thanks Damian,

      I'll look more closely at how the sticky hidden fields work with CGI.pm

      -mark

Re: passing information through hidden fields using CGI.pm
by geektron (Curate) on Feb 13, 2001 at 12:13 UTC
    if you're using CGI.pm and the values change, don't forget to delete the old value(s):
    my $cgi - CGI->new(); $cgi->delete('foo'); $html .= $cgi->hidden( -name => 'foo', -value => $value );

    CGI.pm will append the values, not replace them. i almost got bitten by this earlier today.