Forum Moderators: coopster & phranque

Message Too Old, No Replies

HTML form fields to perl variables

I need help

         

Lennie

5:59 pm on Apr 28, 2003 (gmt 0)

10+ Year Member



I have a form named "test" with text fields inputs
"FirstName" and "LastName".

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.

PsychoTekk

6:50 pm on Apr 28, 2003 (gmt 0)

10+ Year Member



hi Lennie, this should work (just copied it from a script of mine and
deleted some unnecessary stuff)

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

andreasfriedrich

7:06 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In case you are wondering what´s going on in the previous example, here´re some annotations:

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&paraname2=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

PsychoTekk

7:16 pm on Apr 28, 2003 (gmt 0)

10+ Year Member



right, you needed to read two variables...

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

Lennie

6:59 pm on May 1, 2003 (gmt 0)

10+ Year Member



Thank you