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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



sleuth
Enthusiast

Dec 14, 2000, 12:52 AM

Post #1 of 5 (5574 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="http://www.site.com/cgi-bin/form.cgi" method="POST">

Sleuth



japhy
Enthusiast

Dec 15, 2000, 7:54 AM

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

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



sleuth
Enthusiast

Dec 16, 2000, 10:27 PM

Post #3 of 5 (5545 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 CGI.pm way to do this.


Code
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 CGI.pm at Perldoc CGI.pm

Sleuth



japhy
Enthusiast

Dec 16, 2000, 10:31 PM

Post #4 of 5 (5543 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:


Code
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:


Code
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:


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

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

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


sleuth
Enthusiast

Dec 16, 2000, 10:39 PM

Post #5 of 5 (5541 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 ??

Sleuth


 
 


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

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