Link to home
Start Free TrialLog in
Avatar of daveamour
daveamourFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Perl Form Vars

I am learning PErl and am trying to figure out how to grab form posted variables.

I have the following page

***************************************************

sub printHTML
{
  print "<h3>This is a test Perl Script</h3>";
  print "<form method=post action=Default.pl>";
  print "<p>Enter your Firstname <input type=text name=Firstname></p>";
  print "<p>Enter your Surname <input type=text name=Surname></p>";
  print "<p><input type=submit></p>";
  print "</form>";
}

sub printHeader
{
  print "Content-type: text/html\n\n";
  print "<html>";
  print "<head><title>Test Page</title>";
  print "</head>";
  print "<body>";
}

sub printFooter
{
  print "</body>";
  print "</html>";
}

use CGI;

printHeader;
printHTML;

#print "Caller = $ENV{'HTTP_REFERER'}<br /><br />";
#print "Server = $ENV{'SERVER_SOFTWARE'}<br /><br />";
#print "QueryString = $ENV{'QUERY_STRING'}<br /><br />";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

foreach $key (keys(%FORM))
{
    #print "$key : $FORM{$key}<br /><br />";
}

print "$FORM('Firstname')<br /><br />";

printFooter;

***************************************************

This works:

foreach $key (keys(%FORM))
{
    #print "$key : $FORM{$key}<br /><br />";
}

But this doesn't :

print "$FORM('Firstname')<br /><br />";


Any ideas?

Cheers

Dave
Avatar of majorspank
majorspank

I would suggest checking out the man page for the 'CGI' library.  
http://www.univie.ac.at/cgi-demo/cgi-bin/showman.cgi?command=CGI

This will allow you to do things like:

$q = new CGI;
$first_name = $q->param('first_name');
print $first_name;
> #print "$key : $FORM{$key}<br /><br />";
> But this doesn't :
> print "$FORM('Firstname')<br /><br />"

of corse not. It prints:

  Firstname : value-typed-into-form<br><br>

And what is the problem then?
ASKER CERTIFIED SOLUTION
Avatar of majorspank
majorspank

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
damn, didn't recognize the brackets ...
Avatar of daveamour

ASKER

Thanks, I need glasses I think :)

Dave