Home > Archive > PERL CGI Beginners > January 2007 > cgi.pm form save to .html
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
cgi.pm form save to .html
|
|
| Chris Henderson 2007-01-09, 9:55 pm |
| I'm trying to make a form using cgi.pm the output of which will be
saved to a .html file. But the variables are not parsing correctly. If
I put 3 names in 3 different places they all appear together instead
of each appearing on each individual section. Here's my code:
#!/usr/bin/perl
use CGI qw(:standard);
print header;
print start_html('Rent Form'),
h1('Lab Form'),
h2('Washington'),
start_form,
"Person's Name? ",textfield('name'),
p,
"What's your favorite mail client?",
p,
checkbox_group(-name=>'words',
-values=>['mutt','mail','pico','sylpheed'],
-defaults=>['mutt','mail']),
p,
"Editor ",
popup_menu(-name=>'heh',
-values=>['emacs','vi','ed','cat']),
p,
h2('Maryland'),
start_form,
"Person Name? ",textfield('name'),
p,
"What's your favorite mail client?",
p,
checkbox_group(-name=>'words',
-values=>['mutt','mail','pico','sylpheed'],
-defaults=>['mutt','mail']),
p,
h2('New York Division'),
start_form,
"Person Name? ",textfield('name'),
p,
"What's your favorite mail client?",
p,
checkbox_group(-name=>'words',
-values=>['mutt','mail','pico','sylpheed'],
-defaults=>['mutt','mail']),
p,
submit,
end_form,
hr;
open(FH, ">/tmp/result.html") or die "can't append to
html file: $!";
print FH start_html('Rent Form');
print FH h1('Lab Form');
print FH h2('Washington');
print FH "tenant name is", em(param('name'));
print FH h2('Maryland');
print FH "tenant name is", em(param('name'));
print FH h2('New York');
print FH "tenant name is", em(param('name'));
flock(FH, 2) or die "can't flock formlog: $!";
# either using the procedural interface
# use CGI qw(:standard);
save_parameters(*FH); # with CGI::save
# or using the object interface
use CGI;
$query = CGI->new();
$query->save(*FH);
close(FH) or die "can't close formlog: $!";
print end_html;
| |
| Purl Gurl 2007-01-10, 3:55 am |
| Chris Henderson wrote:
(snipped)
> I put 3 names in 3 different places they all appear together instead
> of each appearing on each individual section. Here's my code:
No, you are inserting all three names into one variable, "name"
per your instructions.
> "Person's Name? ",textfield('name'),
> "Person Name? ",textfield('name'),
> "Person Name? ",textfield('name'),
"Person's Name? ",textfield('name_1'),
"Person Name? ",textfield('name_2'),
"Person Name? ",textfield('name_3'),
> open(FH, ">/tmp/result.html") or die "can't append to html file: $!";
This does NOT append. This overwrites.
open(FH, ">>/tmp/result.html") or die "can't append to html file: $!";
This appends.
> print FH "tenant name is", em(param('name'));
> print FH "tenant name is", em(param('name'));
> print FH "tenant name is", em(param('name'));
print FH "tenant name is", em(param('name_1'));
print FH "tenant name is", em(param('name_2'));
print FH "tenant name is", em(param('name_3'));
> flock(FH, 2) or die "can't flock formlog: $!";
Too late. File is already written without being locked.
Lock your file BEFORE you write to your file.
Use a unique variable for each name. Change your write
mode to append. Lock your file before writing.
Purl Gurl
| |
| Sean Davis 2007-01-10, 7:56 am |
| On Tuesday 09 January 2007 22:22, Chris Henderson wrote:
> I'm trying to make a form using cgi.pm the output of which will be
> saved to a .html file. But the variables are not parsing correctly. If
> I put 3 names in 3 different places they all appear together instead
> of each appearing on each individual section. Here's my code:
I think you'll probably need to spend some time reading about CGI and how it
works. You have several start_form()'s in the HTML. That isn't valid.
Having three fields called "name" won't get you what you want, either; each
field has to have a unique name.
As for the code itself, remember that CGI is a stateless protocol, so your
program cannot run and wait for user input, and then continue. It has to
output the form for input (at which the program will end). Then, the user
will put some values into the form and click "submit". Then your program
will be run AGAIN, at which point you need to determine if your program needs
to generate the form or process the form. This is the part that is
mind-bending, but is absolutely necessary to understand.
I suggest spending some time reading and doing some tutorials. A reasonably
good one in perl is here:
http://users.easystreet.com/ovid/cgi_course/
Hope that helps some.
Sean
|
|
|
|
|