How should I instruct the (submit button) 'onClick' so that it directs the page to place these two fields into a perl variable?
In short, I want to know how to make:
$First=document.test.FirstName.value
$Last=document.test.LastName.value
$First and $Last are my perl variables.
for the html page use
<form method="POST" enctype="application/x-www-form-urlencoded" action="cgi-bin/target.cgi">
Login:<input type="password" name="somename">
<input type="submit" value="OK">
</form>
for the perl script (in this case /cgi-bin/target.cgi) use
read(STDIN, my $Data, $ENV{'CONTENT_LENGTH'});
(my $Name, my $Val) = split(/=/, $Data);
$Val =~ tr/+/ /;
$Val =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$Data{$Name} = $Val;
my $Login = $Data{somename};
The Common Gateway Interface specifies that POSTed data be passed to the handling process on STDIN. The environment variable CONTENT_LENGTH contains the length of the POSTed data.
read [perldoc.com](STDIN, my $data, $ENV{'CONTENT_LENGTH'});
reads $ENV{'CONTENT_LENGTH'} bytes from the filehandle STDIN into the scalar variable $data. This data is in the format paraname1=value1¶name2=value2 and still urlencoded. See RFC1738 - Uniform Resource Locators (URL) [faqs.org] for how that is done.
Thus we would first split [perldoc.com] $data on & to separate parameter - value pairs. In the above example there is only one such pair. Therefor we can immediately split [perldoc.com] $data on =.
(my $name, my $val) = split [perldoc.com](/=/, $data);
Since the $value is still urlencoded (the $name would be as well) we need to decode it.
$val =~ tr [perldoc.com]/+/ /;
translates + back to spaces. Then
$val =~ s [perldoc.com]/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
turns the %hex_codes back into ordinary characters.
Andreas
read(STDIN, my $Data, $ENV{'CONTENT_LENGTH'});
my @Formfields = split(/&/, $Data);
my ($Field, $Name, $Value);
my %Form;
foreach $Field (@Formfields)
{(my $Name, my $Value) = split(/=/, $Field);
$Value =~ tr/+/ /;
$Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$Form{$Name} = $Value;
my $Var1 = $Form{Var1};
my $Var2 = $Form{Var2};}