How do I decode information from HTML forms?
Contributors:
- JimEsten jesten@wdynamic.com
When the users clicks the submit button the information
is transmitted as a URL encoded string.
This string is in the format:
key=value&key=value&key=value
...where each key (the "name" of your form element) and value (what the user inputs) are separated by the ampersand. The key and the value themselves are separated by an equal sign. Any non alpha-numeric characters get represented by a hexadecimal equivalent preceded by a percent sign.
So, to decode, first separate each key/value pair, then separate the keys from the values, and finally convert any characters that aren't already readable.
Here is a generic perl routine that leaves the contents
of the form in an associative array %FORM
sub parse {
local($name, $value, $pair, $buffer, @pairs);
# Check for request method and handle appropriately
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
$Error_Message = "Bad request method ($ENV{'REQUEST_METHOD'}). Use POST or GET";
return(0);
}
# Convert the data to its original format
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~ s/\n//g;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\n//g;
$FORM{$name} = $value;
}
}
This can be used directly in the cgi script, or placed in a separate file and "require" that file in each script that parses a form.
(There are also existing libraries of free code to carry out this and other CGI-related tasks in many programming languages. -TB)
Previous | Next | Table of Contents
Follow us on Twitter | Contact Us
Copyright 1994-2012 Boutell.Com, Inc. All Rights Reserved.