JavaScript EditorFreeware JavaScript Editor     Perl Tutorials 



Main Page Previous Section Next Section

Recipe 21.6 Accessing Form Parameters

21.6.1 Problem

You want the values for form fields submitted by the client.

21.6.2 Solution

To access the form's various parameters, use $r->content to access POSTed parameters and $r->args to access GET parameters encoded in the URL.

%post_parameters = $r->content;
%get_parameters  = $r->args;

You can call $r->content only once per request because the first call consumes all POSTed data.

The Apache::Request module from CPAN gives you a $r->param method to access specific parameters, regardless of whether they're from GET or POST:

use Apache::Request;

sub handler {
  my $r = Apache::Request->instance(shift);
  my @param_names = $r->param;
  my $value       = $r->param("username");  # single value
  my @values      = $r->param("toppings");  # multiple values

  # ...
}

21.6.3 Discussion

Processing form parameters without Apache::Request is problematic with values that occur multiple times. For example, a SELECT list with MULTIPLE enabled sends repeated entries for the same parameter name. Putting them into a hash preserves only one of those entries. Apache::Request solves this problem by accumulating multiply-submitted parameters in an array.

Form parameters POSTed to your handler can be a problem. The nature of Apache is that once one handler reads the POSTed data, another handler cannot come along later and reread that same information. So if you're going to process POSTed form parameters, you had better keep the decoded parameters around in case another handler wants to access them. The instance constructor handles this for us. When two handlers both call the instance constructor, the second handler gets back the Apache::Request object populated by the first, with form parameters already decoded.

The Apache::Request $r->param interface is based on the CGI module's parameter-parsing interface.

21.6.4 See Also

The Apache.pm manpage; Writing Apache Modules with Perl and C; Recipe 3.5 in mod_perl Developer's Cookbook; the Apache::Request manpage; Recipe 20.2

    Main Page Previous Section Next Section
    


    JavaScript EditorJavaScript Verifier     Perl Tutorials


    ©