Click to See Complete Forum and Search --> : POST and perl problem


doness
02-01-2003, 03:52 AM
Problem with Forms and POST method. I am another newbie to CGI and perl programs
I have downloaded Apache and Perl 5 from Activestate, and bought 3 books!
I am trying to get the POST method to send a small amount of data from a form via
CGI and my perl program to a file. (I managed to get the GET method to work,
but I need to be able to use POST for larger amts of data).
The perl script is:
#!c:/Perl/bin/Perl.exe
##
use CGI;
##
$query = new CGI;
##
$Filnam="DonsPOST";

open (REC, ">$Filnam");

$xxx ="Hello Dolly";
$yyy="Extra bit";

if($ENV{'REQUEST_METHOD'} eq 'POST'){
read (STDIN, $xxx, $ENV { 'CONTENT_LENGTH'} );
# write data to file
print REC $string;
close (REC);
}
$xxx=$xxx.$yyy;
print $query->header;
print "<html><head><title> TestPOST</title></head>\n";
print "<body>$xxx</body></html>\n";
eof;

The explorer HTML is:

<HTML><HEAD><TITLE> Dons Test form</title ></head>
<body><h1 align="left">Visitor info form</h1>
<HR>
<form method="POST" action="/cgi-bin/DsTest.pl">
<B>Last Name:<input type="text" name="LastName" size=16>
First Name:<input type="text" name="FirstName" size=16>
<br><BR>
State: <input type "text" name="address" size=32>
City:<input type "text" name="city" size=32>
<br><br>
<center>
<input type="submit" value="sendinfo">
<input type="reset" value="clear form fields">
</b></form></body></html>

Running this in explorer returns the new form with "Extra bit" typed on it,
but none of the $xxx data that I entered into the fields.
It seems that $xxx which had "Hello Dolly" in it has been zeroed by the attempt
to read the STDIN into it.
A file called DonsPost has been opened and closed, but it has zero bytes - again the
data I entered in the HTML boxes has not been transferred to $xxx.

If I change the method to "GET" and the perl to $xxx=$ENV{'QUERY_STRING'}; I get all
of the data returned properly to the opened file.

What am I doing wrong?

Thanks in advance, I have spent three days trying to solve this!

Don

Charles
02-01-2003, 07:21 AM
You need to read the CGI.pm module documentation and you need to use the param method. See http://www.perldoc.com/perl5.8.0/lib/CGI.html.

doness
02-04-2003, 02:28 PM
Thanks for the help - i was mixing up cgi.pm type instructions with non cgi.pm

I have downloaded the 51 pages re cgi.pm but info is difficult to follow unless you have prev knowledge - any other web pages with more basic cgi.pm info?

Thanks

Don

nkaisare
02-04-2003, 09:41 PM
The one charles mentioned is the one I used when I got started with CGI. It isn't that tough, just stay put. Try and make mistakes... thats the best way to learn.

If you have any form element named "abcdef", you can use param('abcdef') to refer to the value passed to it when form was submitted.

Also, when you use the method "GET", the URL will be something like
"http://whereever.com/whatever.cgi?abcdef=ghijkl"
Thus, $xxx will have content "abcdef=ghijkl".

If you use "POST", the url will be
"http://whereever.com/whatever.cgi"
and $xxx will be blank.

doness
02-06-2003, 02:00 PM
Originally posted by nkaisare
The one charles mentioned is the one I used when I got started with CGI. It isn't that tough, just stay put. Try and make mistakes... thats the best way to learn.

If you have any form element named "abcdef", you can use param('abcdef') to refer to the value passed to it when form was submitted.

Also, when you use the method "GET", the URL will be something like
"http://whereever.com/whatever.cgi?abcdef=ghijkl"
Thus, $xxx will have content "abcdef=ghijkl".

If you use "POST", the url will be
"http://whereever.com/whatever.cgi"
and $xxx will be blank.

Thanks for your encouragement! I have downloaded over 200 pages from 2 sites and have found (some) of my mistakes. At present I am testing out the $query->import_names('R') method, and this seems to work very well.

Thanks again

Don

Paco Zarabozo
02-07-2003, 04:16 PM
It's this simple:
_________________________________
#!perl
use CGI;
$q = new CGI;

print "Content-type:text/html\n\n";

foreach $i ($q->param()) {
$f{$i} = $q->param($i);
}
_________________________________

That would assign any filed to the hash %f. It doesn't matter anything else, no matter what method you used and you don't need to unpack anything. I mean, if you sent a field named "username", at this point you'd access it using:
_________________________________

print "Your username is $f{username}";
_________________________________

...or, if you need to print on the screen every field without having to know the name of every field:
_________________________________
foreach $i (keys(%f)) {
print "$i = $f{$i}<br>";
}
_________________________________
Need to know what method was used? That information is on $ENV{REQUEST_METHOD}.
Need to know if the form wasn't send? Use this:
_________________________________
if (!$q->param()) {
print "You didn't send anything.";
}
_________________________________


I hope this to help.

Paco.

Charles
02-07-2003, 04:32 PM
If yuo are going to use the CGI.pm module, then use the CGI.pm module.

#!usr/local/bin/perl

use CGI;
my $q = new CGI;

my $title = 'My Happy Script';

if ($q->param('username')) {print $q->header(), $q->start_html($title), $q->h1($title), $q->p('Your username is', $q->param(), '.'), $q->end_html} else {print $q->header(), $q->start_html($title), $q->h1($title), $q->p('Please log in.'), $q->end_html};

Or, more simply:

#!usr/local/bin/perl

use CGI qw(:all);

my $title = 'My Happy Script';

if (param('username')) {print header(), start_html($title), h1($title), p('Your username is', param(), '.'), end_html} else {print header(), start_html($title), h1($title), p('Please log in.'), end_html};

Paco Zarabozo
02-07-2003, 04:49 PM
It's a matter of preference. For instance, it's well known it's never a good idea to just import everything unless you're using it.

Now, i personally just don't like cgi.pm html functions (start_html and so). I has to do with the fact that i use my own html module, so i have my own header(), etc.

And, in the previouslly given example, you get all the form in a hash in three lines. You can have more use from a hash from the beginning than using $q->param all the time.

But, like i said, it's a matter of preference. :-)

Paco.

jeffmott
02-08-2003, 02:57 PM
Paco Zarabozo
Now, i personally just don't like cgi.pm html functions
What is it you don't like about them?

And, in the previouslly given example, you get all the form in a hash in three lines
Why not do it in one line?

my %f = $q->Vars;

You can have more use from a hash from the beginning than using $q->param all the time
What can you do with a hash that you can't do with $q->param()?

Paco Zarabozo
02-08-2003, 03:39 PM
Originally posted by jeffmott
What is it you don't like about them?

Well; i like to use the exactly html code i want to use. That includes headers. Sometimes, for some things, i prefere to use only the content type line. Besides, i'm more confortable with my own shortcuts to certain functions. For example, my buttons: button(Submit).


Why not do it in one line?

my %f = $q->Vars;


I gotta say i didn't know that method. Thanks.


What can you do with a hash that you can't do with $q->param()?

For starters:

print "Your name is $f{name}";
That works.

print "Your name is $q->param('name')";
That doesn't work. Besides, it's a little more difficult to write fast.

The same effect occurs using other kind of assignations, like using DBI.

Paco.

doness
02-09-2003, 03:33 AM
Originally posted by Paco Zarabozo
It's this simple:
_________________________________
#!perl
use CGI;
$q = new CGI;

print "Content-type:text/html\n\n";

foreach $i ($q->param()) {
$f{$i} = $q->param($i);
}
_________________________________

That would assign any filed to the hash %f. It doesn't matter anything else, no matter what method you used and you don't need to unpack anything. I mean, if you sent a field named "username", at this point you'd access it using:
_________________________________

print "Your username is $f{username}";
_________________________________

...or, if you need to print on the screen every field without having to know the name of every field:
_________________________________
foreach $i (keys(%f)) {
print "$i = $f{$i}<br>";
}
_________________________________
Need to know what method was used? That information is on $ENV{REQUEST_METHOD}.
Need to know if the form wasn't send? Use this:
_________________________________
if (!$q->param()) {
print "You didn't send anything.";
}
_________________________________


I hope this to help.

Paco.

Thanks very much for all your help - all very helpful - I think I have staggered onto this solution by reading the dowmloads from cgi.pm. I will try to get some sleep now!
Cheer Don

indiranmunna
01-10-2006, 10:31 AM
I got solution from u r article, thank u a lot.