Re: sending @array as hidden variable

2001-08-26 Thread Curtis Poe

--- "Simon K. Chan" <[EMAIL PROTECTED]> wrote:
> 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.

Simon,

No problem about sending the script.  It's short and the problem was easy to spot.

When sending data via a POST request, the data is available through STDIN.  Unless you 
do some
really fancy footwork with Perl, that data is only available to be read once.  Here's 
what's
causing the  problem:

&ReadParse(*input);
my @array = param('food');

Once you start using CGI.pm, you can drop the &ReadParse.  That reads the data in 
STDIN and CGI.pm
has no data left to read.  Get rid of that and it should work fine.

Also, the &ReadParse routine has many bugs in it.  I won't go into it at length right 
now, but
it's seen all over the place and, unfortunately, it's resulted in a lot of buggy code 
out there. 
Your content type line is also incorrect:

print "Content-type:text/html\n\n";
 
That should have a space between the colon and the word 'text'.  If it works for you, 
you're
benefitting from error correction that many browsers have built in.  It's such a 
common error that
browsers often figure it out.  Since you're using CGI.pm, you can just use the 
following:

print header();

Incidentally, I know of at least one case where the above line resulted in a 
"text/plain" header. 
To force the correct header, if that happens, you can do this:

print header( -type => 'text/html' );

Cheers,
Curtis "Ovid" 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]




Re: sending @array as hidden variable

2001-08-26 Thread Simon K. Chan

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
foreach $item (@food){
print qq{};
}
print "";



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 "";}

### 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 "";
> > }
> > 
> > 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{\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{\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]




Re: sending @array as hidden variable

2001-08-24 Thread Curtis Poe

--- "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 "";
> }
> 
> 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{\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{\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]




sending @array as hidden variable

2001-08-23 Thread Simon K. Chan

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 "";
}

My two questions are:

1.  Is this the most efficient way of sending an array as a hidden value?

2.  And if so, what is the corresponding $input{''} syntax?

Many thanks for your time, everybody!  



=
#
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]