How about a little perl string manipulation?

#my $a = 'key1=value1&key2=&key3=value3';
my $a = 'key1=value1&key2&key3=value3';

my $b = join "&", map { my $c = ($_ =~ m/(\S+)=(\S*)?/o) ? $_ : "$_="; $c; } 
split "&", $a;

print "a= $a\nb= $b\n";

-James

-----Original Message-----
From: Randolf Richardson [mailto:rand...@modperl.pl]
Sent: Tuesday, January 03, 2012 1:13 PM
To: mod_perl list
Subject: Re: Cannot retrieve empty keys from request using 
Apache2::Request.param

> Tommaso Torti wrote:
> > Hi all,
> >
> > i'm using mod_perl in order to retrieve all keys from a POST request with:
> >
> > my $request = Apache2::Request->new($r);
> > my $data = $request->param();
> >
> > When i receive something like
> > key1=value1&key2=&key3=value3
> > $data contains all 3 keys
> >
> > but with
> > key1=value1&key2&key3=value3
> > $data contains only 2 keys: key1 and key2&key3
> >
> > Is there a way to retrieve all the 3 keys like the first case? Any 
> > suggestions?
> > Thank you,
>
> As far as I know, there isn't a way, because the browser will not send any 
> data for an
> input field that is empty in the <form>.
>
> If you want to do that, then you would need some javascript inside the form, 
> which fills
> the empty fields with some "dummy" sequence (like "--empty--"), before 
> posting the form.
> Then your module/script would need to catch such fields, and consider them as 
> empty.

        Interesting.  I suppose one could use the GET method to verify
whether $request->param() shoots blanks or undefs.

        Filling in fields seems like a lot of work though, and using
JavaScript to fill in those fields with dummy data won't be
compatible with anything.  A better approach would be to just include
a hidden input element with the same name at some point after the
user's input field:

                <input type=hidden name=key2 value=--empty-->

        But, I'd rather use this approach instead, if possible:

                <input type=hidden name=key2 value="">

        The beauty of this solution is that calling $request->param() in
scalar context returns the first value, but if the POST method really
isn't passing along blanks then the hidden input element's value will
be in first place instead.

        However, I strongly suspect that this really is not the right
solution.

Randolf Richardson - rand...@inter-corporate.com
Inter-Corporate Computer & Network Services, Inc.
Beautiful British Columbia, Canada
http://www.inter-corporate.com/



IMPORTANT NOTICE REGARDING THIS ELECTRONIC MESSAGE:

This message is intended for the use of the person to whom it is addressed and 
may contain information that is privileged, confidential, and protected from 
disclosure under applicable law.  If you are not the intended recipient, your 
use of this message for any purpose is strictly prohibited.  If you have 
received this communication in error, please delete the message and notify the 
sender so that we may correct our records.

Reply via email to