For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > May 2007 > Passing a hash from a form field?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing a hash from a form field?
perl4hire@softouch.on.ca

2007-04-30, 7:55 am

I have several instances where I would like to pass a hash (or a hash
reference) in a hidden form field to a subroutine within the same script
upon submitting the form.

So far all my attempts have failed. I'm not even sure if this is
possible. I know about passing named parameters and references but that
only seems to work if the subroutine is called directly, not due to a
form submission. Does anyone know if this is even possible, and if so,
some direction I can take?

#! /usr/bin/perl
BEGIN
{
open (STDERR,">>$0-err.txt");
print STDERR "\n",scalar localtime,"\n";
}

use strict;
use warnings;
use CGI ':standard';

my %Categories =(
1 => "Exterior",
2 => "Interior",
3 => "Audio",
4 => "Vehicles",
5 => "Packages"
);
my $form_url= url();

print "Content-type: text/html\n\n";

my $MODE=param('Mode');
{
no strict;
$MODE="PageOne" unless $MODE; # default sub to execute
&$MODE; # else execute this
}
########################################
######
sub PageOne
{
print "<html><body>";

foreach my $k ( sort keys %Categories)
{
print "[$k] => $Categories{$k}<br>";
}

print <<EndHTML;
<form method="post" action="$form_url">
<input type="hidden" name="Categories" value="%Categories">
<input type="hidden" name="Mode" value="ReadCat">
<input type="submit">
</form>
</body>
</html>
EndHTML
}
########################################
##########
sub ReadCat
{
my %Cats = param('Categories');
print "<html><body>";
foreach my $k (keys %Cats)
{
print "[$k] => $Cats{$k}<br>\n";
}
print "</body></html>\n";
}
Amooaeergh

2007-04-30, 9:52 am

Cutie teasing pusy with vibro then facking hard
http://trully-bigtits.info/play.asp?bigtits218571
Bill Stephenson

2007-04-30, 9:55 pm

Take a look at the example scripts and docs...

http://search.cpan.org/src/LDS/CGI....ples/index.html

http://search.cpan.org/src/LDS/CGI....9/cgi_docs.html

I'm pretty sure you'll find the syntax you need in those documents.

Kindest Regards,

--
Bill Stephenson


On Apr 29, 2007, at 2:02 PM, perl4hire@softouch.on.ca wrote:

> I have several instances where I would like to pass a hash (or a hash
> reference) in a hidden form field to a subroutine within the same
> script upon submitting the form.
>
> So far all my attempts have failed. I'm not even sure if this is
> possible. I know about passing named parameters and references but
> that only seems to work if the subroutine is called directly, not due
> to a form submission. Does anyone know if this is even possible, and
> if so, some direction I can take?
>
> #! /usr/bin/perl
> BEGIN
> {
> open (STDERR,">>$0-err.txt");
> print STDERR "\n",scalar localtime,"\n";
> }
>
> use strict;
> use warnings;
> use CGI ':standard';
>
> my %Categories =(
> 1 => "Exterior",
> 2 => "Interior",
> 3 => "Audio",
> 4 => "Vehicles",
> 5 => "Packages"
> );
> my $form_url= url();
>
> print "Content-type: text/html\n\n";
>
> my $MODE=param('Mode');
> {
> no strict;
> $MODE="PageOne" unless $MODE; # default sub to execute
> &$MODE; # else execute this
> }
> ########################################
######
> sub PageOne
> {
> print "<html><body>";
>
> foreach my $k ( sort keys %Categories)
> {
> print "[$k] => $Categories{$k}<br>";
> }
>
> print <<EndHTML;
> <form method="post" action="$form_url">
> <input type="hidden" name="Categories" value="%Categories">
> <input type="hidden" name="Mode" value="ReadCat">
> <input type="submit">
> </form>
> </body>
> </html>
> EndHTML
> }
> ########################################
##########
> sub ReadCat
> {
> my %Cats = param('Categories');
> print "<html><body>";
> foreach my $k (keys %Cats)
> {
> print "[$k] => $Cats{$k}<br>\n";
> }
> print "</body></html>\n";
> }
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> http://learn.perl.org/
>
>


Shaelynn

2007-05-01, 4:43 am

Martha Stewart in anal action!
http://Martha-Stewart-anal-action.o...hp?movie=148803
aneely@softouch.on.ca

2007-05-01, 6:55 pm

Bill Stephenson wrote:
> Take a look at the example scripts and docs...
>
> http://search.cpan.org/src/LDS/CGI....ples/index.html
>
> http://search.cpan.org/src/LDS/CGI....9/cgi_docs.html
>
> I'm pretty sure you'll find the syntax you need in those documents.
>
> Kindest Regards,
>
> --
> Bill Stephenson


I actually have the book as well ('Official Guide to Programming with
CGI.pm') and have practically torn it apart trying to find a way to do
what I want. I've tracked down and read a number of articles and forum
postings as well.

I have a feeling that a hash can't be passed this way. Arrays and
strings are no problem, but I believe the CGI protocol does not allow
passing complex data structures like arrays of hashes, or hashes of hashes.
[color=darkred]
>
>
> On Apr 29, 2007, at 2:02 PM, perl4hire@softouch.on.ca wrote:
>
Mumia W.

2007-05-01, 6:55 pm

On 04/30/2007 11:03 PM, aneely@softouch.on.ca wrote:
>
> I actually have the book as well ('Official Guide to Programming with
> CGI.pm') and have practically torn it apart trying to find a way to do
> what I want. I've tracked down and read a number of articles and forum
> postings as well.
>
> I have a feeling that a hash can't be passed this way. Arrays and
> strings are no problem, but I believe the CGI protocol does not allow
> passing complex data structures like arrays of hashes, or hashes of hashes.
>


You are correct that CGI does not allow you to pass HoH structures
directly; however, you can use Storable.pm to convert a HoH into a
string that can be made the value of a form element (after it's been
HTML-encoded).

Sessions are an even better idea for passing complex data between CGI
programs written by the same author. Investigate the docs for
CGI::Session and Apache::Session (if you're using Apache).

aneely@softouch.on.ca

2007-05-02, 3:55 am

Mumia W. wrote:
> On 04/30/2007 11:03 PM, aneely@softouch.on.ca wrote:
>
> You are correct that CGI does not allow you to pass HoH structures
> directly; however, you can use Storable.pm to convert a HoH into a
> string that can be made the value of a form element (after it's been
> HTML-encoded).
>
> Sessions are an even better idea for passing complex data between CGI
> programs written by the same author. Investigate the docs for
> CGI::Session and Apache::Session (if you're using Apache).
>


Ahh, thank you. I will check out both of those.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com