CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Frequently Asked Questions:
How Do I Read Data From A Form?



Dec 14, 2000, 12:52 AM

Post #1 of 5 (11057 views)
How Do I Read Data From A Form? Can't Post

 First, you have to put this at the top of your code,

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@cgiPairs = split(/&/,$buffer);
foreach $cgiPair (@cgiPairs)
($name,$value) = split(/=/,$cgiPair);
$value =~ s/\+/ /g;
$value =~ s/%(..)/pack("c",hex($1))/ge;
$form{$name} .= "\0" if (defined($form{$name}));
$form{$name} .= "$value";

And then all of the form values will be stored in the hash %form. So if your field name in your form is named "text" then all you have to do to manipulate that information, is use $form{'text'}, and say "text" is just a text box and you typed in "this is text", then in your program you could,

print "$form{'text'}";

And it would print

this is text

So for every field name you have in the form that you submit to your script, it will be read into $form{'field_name'}.

In your form, make sure that you don't forget to set the action to the url of your script,

<form action="" method="POST">



Dec 15, 2000, 7:54 AM

Post #2 of 5 (11044 views)
Re: How Do I Read Data From A Form? [In reply to] Can't Post

I would STRONGLY recommend you use the module instead of code on your own. There are so many things that can go wrong. is kid-tested, mother-approved.


Dec 16, 2000, 10:27 PM

Post #3 of 5 (11028 views)
Re: How Do I Read Data From A Form? [In reply to] Can't Post

 He he he, Ok Japhy, your not the only one who tells me I should. Here is the way to do this.

use CGI qw/:standard/; 
# and instead of $form{'text'}
print "param('text')";

This module alone allows you to read a forms input in a variety of ways as well, you can read more about how to use at Perldoc



Dec 16, 2000, 10:31 PM

Post #4 of 5 (11026 views)
Re: How Do I Read Data From A Form? [In reply to] Can't Post

Function calls do not expand in quotes -- you need to do:

print "foo's value is ", param("foo"), "\n"; 

# or

$value = param("foo");
print "foo's value is $value\n";

Or, you can use the deref-array-ref trick, which creates a reference to an anonymous array, and then dereferences it inside a double-quoted string. I'll show it as a whole, and then break it down:

print "foo's value is @{[ param("foo") ]}\n";

The trick is that arrays are interpolated inside double quotes, and therefore, if you can put the stuff you want printed inside an array, then you can print it. And we can turn anything into an array by putting it inside a REFERENCE to an array, and then dereferencing it:

print "@{  # printing an array, and dereferencing... 
[ # a reference to an array...
param("foo") # containing this function's return value

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

(This post was edited by japhy on Dec 16, 2000, 9:43 PM)


Dec 16, 2000, 10:39 PM

Post #5 of 5 (11024 views)
Re: How Do I Read Data From A Form? [In reply to] Can't Post

Did you have any problems before with that, because I just tested it and it works ??



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives