Curtis,
Your suggestion does not seem to work for me. My apologies for sending you the
script, but the
my @food = param('food'); does not work for me! Any and all suggestions would be much
appreciated.
here is the ovid1.cgi:
#!/usr/local/bin/perl
use CGI qw/:standard/;
print "Content-type:text/html\n\n";
@food = ("bread", "ham", "eggs");
print <<EndHTML;
<form method=post action="http://cgi.sfu.ca/~skchan/cgi-bin/ovid2.cgi">
<input type="submit" value="click">
EndHTML
foreach $item (@food){
print qq{<input type="hidden" name="food" value="$item">};
}
print "</form>";
####################
And here is ovid2.cgi:
#!/usr/local/bin/perl
use strict;
use CGI qw/:standard/;
print "Content-type:text/html\n\n";
&ReadParse(*input);
my @array = param('food');
my $array;
foreach $array (@array){
print "$array"
print "<BR>";}
### ReadParse #####################################################################
sub ReadParse {
local (*in) = @_ if @_;
###reads the form inputs and sorts. DON'T CHANGE!!!
local ($i, $loc, $key, $val);
if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; }
elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});}
@in = split(/&/,$in);
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g;
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;
}
return 1; # just for fun
}
--- Curtis Poe <[EMAIL PROTECTED]> wrote:
> --- "Simon K. Chan" <[EMAIL PROTECTED]> wrote:
> > Hi Everybody,
> >
> > I am trying to send an array as a hidden value in my script as follows:
> >
> > @array = (eggs, ham, bread, pop);
> >
> > foreach $food (@array){
> > print "<INPUT TYPE=\"hidden\" NAME=\"food\" VALUE=\"$food\">";
> > }
> >
> > My two questions are:
> >
> > 1. Is this the most efficient way of sending an array as a hidden value?
>
> Hello Simon,
>
> What do you mean by 'efficiency'? You could mean "most efficient" in terms of
>program speed,
> bandwidth, or ease of use. I am assuming that you mean ease of use. If so, I would
>say "yes",
> you have the most efficient method. However, it's not very robust. Consider the
>following:
>
> use strict;
> my @array = ( 'Ovid', 'Fred Flintstone', 'Barney "the wimp" Rubble' );
> foreach my $name ( @array ) {
> print qq{<INPUT TYPE="hidden" NAME="name" VALUE="$name">\n};
> }
>
> In the above example, the HTML will be broken by the quotes marks embedded in Barney
>Rubble's
> name. To deal with that, use HTML::Entities. That will encode your data so it
>doesn't mess up
> your forms.
>
> use strict;
> use HTML::Entities;
> my @array = ( 'Ovid', 'Fred Flintstone', 'Barney "the wimp" Rubble' );
> foreach my $name ( @array ) {
> $name = encode_entities( $name );
> print qq{<INPUT TYPE="hidden" NAME="name" VALUE="$name">\n};
> }
>
> > 2. And if so, what is the corresponding $input{''} syntax?
>
> use strict;
> use CGI qw/:standard/;
> my @food = param( 'food' );
>
> Note that when using CGI.pm, the CGI::param function uses 'wantarray' to see if
>you're expecting
> a
> scalar or an array returned. If you were to do this:
>
> my $food = param( 'food' );
>
> CGI.pm would only give you the first food item. Using '@food' gives you all foods
>listed.
>
> Cheers,
> Curtis Poe
>
> =====
> Senior Programmer
> Onsite! Technology (http://www.onsitetech.com/)
> "Ovid" on http://www.perlmonks.org/
>
> __________________________________________________
> Do You Yahoo!?
> Make international calls for as low as $.04/minute with Yahoo! Messenger
> http://phonecard.yahoo.com/
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
=====
#########################################
Warmest Regards,
Simon K. Chan - [EMAIL PROTECTED]
"Great spirits have always encountered violent opposition from mediocre minds."
-Albert Einstein
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]