Click to See Complete Forum and Search --> : Form, information, print


harkrank
02-20-2004, 02:24 PM
I'm trying to find a solution for a probably very simple problem to most of you.

This is what i want to do:

by filling out a form (form.html) and pushing submit you are supposed to see the result on the next page (result.html) When you hit submit, you're beeing redirected to the result (result.html) which contains the data you filled in the form on the previous page.

This shouldn't be that hard if you have any skills, but all I know about Cgi is how to CHMOD, execute and where to put them =)

By the way, the code need to be written in Perl. The results does not have to be saved anywhere on the server, only printed as explained above.

Thanks a lot!

---
http://www.harkrank.se

Nedals
02-20-2004, 09:23 PM
#!/usr/bin/perl
#=========================================
# result.cgi
# This is a template I use.
#
#===========================================
# If you have problems, check your 'path to perl' above (#!/usr/bin/perl)
# Put this script in cgi-bin and CMOD to 755
# You call the script in your 'FORM.HTML' doc using...
# <form action="/cgi-bin/result.cgi" method="post"> NOT 'RESULT.HTML'
#===========================================
use strict # You should always use this. It forces you to declare variables with 'my'
use CGI; # a module to help get the form data
use CGI::Carp(qw/fatalsToBrowser/); # reports errors

my $q = new CGI;
my %cgi_vars = $q->Vars(); # Collects all the form data and puts it in a HASH

# Say you have 2 form elements named (and they MUST be named) 'user' and 'address'.
# This data will now be contained in
# $cgi_vars{'user'}
# $cgi_vars{'address'}
# So now insert these into your docHTML (see below)
#
# Theres's lots to learn about Perl.
# I've been at it for a couple of years and I'm still a relative novice.
# There are many other methods to do this, but I think this is the
# easiest to understand.

#========================== S T A R T M A I N P R O G R A M
# this prints the required header when returning an HTML doc
# ( "Content-Type: text/html\n\n"; )
print $q->header();
&docHTML(); # call the subroutine
exit;



#================================ S U B R O U T I N E S


#===================== R E T U R N E D H T M L D O C U M E N T S
sub docHTML {
print <<HTML;
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html><head><title>Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
User name: $cgi_vars{'user'}
Address: $cgi_vars{'address'}
</body>
</html>
HTML
}

CyCo
02-21-2004, 09:08 AM
...or something like this...


#!/usr/bin/perl

use strict;
use CGI qw/:standard/;

my ($key,@values);

print header,
start_html('Form Results'),
h3('Form Results');
foreach $key(param) {
@values = param($key);
print $key.': ',
join(', ',@values),
br;
}
print end_html;


# #
# THIS IS A VERY SIMPLE EXAMPLE OF USING CGI.PM TO PARSE ANY #
# FORM. THIS ONE UTILIZES THE FUNCTION-ORIENTED STYLE OF CGI #
# PROGRAMMING. I WANT TO POINT OUT THAT THIS IS "NOT" A VAL- #
# ID "USE-ON-THE-WEB" TYPE OF SCRIPT...AS WRITTEN. IT LACKS, #
# AMONG OTHER THINGS, DOMAIN & SERVER VALIDATION, CHARACTER #
# & ERROR CHECKS AND MORE. **** IT'S AN EXAMPLE !! OK ? **** #
# BESIDES, IT DOES NOTHING WITH THE DATA, EXCEPT DISPLAY THE #
# INPUT FIELDNAMES AND THEIR CORRESPONDING VALUES IN AN HTML #
# PAGE GENERATED DYNAMICALLY ON THE FLY. THIS, OF COURSE CAN #
# BE USED AS A BUILDING BLOCK FOR MANY, MANY GENRES OF USER- #
# INTERACTIVE WEB PROGRAMS. DATABASES, EMAILS...YOU NAME IT! #
# #