#!/usr/bin/perl ############################################################################### # writeData: # # 1. Mail the content of the form, if an email address is provided # either at the end of URL, or as a hidden field in the form. # such as:
# or as: # # 2. Save the content of the form in a file, if a file is specified # as a hidden field in the form. # such as: # # 3. Redirect to another html page, if a redirect url is specified # as a hidden field in the form. # such as: ############################################################################### use CGI "param"; # use CGI param modules @elementNames = param(); # get all FORM element names &mailData(); # Call mailData to e-mail &saveData(); # Call saveData to save data &printHTML(); # Call printHTML to print HTML ############################################################################### # mailData: If email address is specified, # then mail the form data to the e-mail address. ############################################################################### sub mailData { $email = $ENV{'PATH_INFO'}; # get email from end of URL $email =~ s'/''; # strip out first '/' $email = param('_email') || param('_EMAIL') || $email; #get email field return if (! $email); # exit if no email specified open(MAIL, "| /usr/sbin/sendmail $email "); # pipe to unix mail command print (MAIL "Subject: Form Element Content \n"); print (MAIL "\n"); print (MAIL "Your Form Elements and Values are:\n"); foreach $name (@elementNames) { # for each form field if ($name =~ /(_email|_file|_redirect)/i) { # if name is one of these next; # skip it } print MAIL "\n $name: "; # send name to mail @values = param($name); # get the values of field foreach $value (@values) { # for each value print MAIL "$value "; # send value to mail } } close(MAIL); } ############################################################################### # saveData: If a file is specified, # then save the form data in the file. ############################################################################### sub saveData { $file = param('_file') || param('_FILE'); # get file name to save data return if (! $file); # exit if no file specified open (OUTPUT , ">> $file") or die("Cannot open output file $file - $!"); foreach $name (@elementNames) { # for each for field if ($name =~ /(_email|_file|_redirect)/i) { # if name is one of these next; # skip it } print OUTPUT "$name=="; # print name of field @values = param($name); # get the values of field foreach $value (@values) { # for each value $value =~ s/\r\n/__/g; # replace newline with __ print OUTPUT "$value##"; # write the field values } print OUTPUT "||"; } print OUTPUT "\n"; close(OUTPUT); } ############################################################################### # printHTML: If a redirect is specified, # then redirect to the requested URL # else generate a simple output HTML page ############################################################################### sub printHTML { $redirect = param('_redirect') || param('_REDIRECT'); # get redirect URL print "Content-type: text/html \n"; if ($redirect) { # if a redirect is requested print "Location: $redirect \n\n"; # create HTTP Location header return; } print "\n"; print " Thank you

Thank you for your request

"; print ""; foreach $name (@elementNames) { # for each form field if ($name =~ /(_email|_file|_redirect)/i) { # if name is one of these next; # skip it } print "\n
$name:"; # print name @values = param($name); # get the values of field foreach $value (@values) { # for each value print "$value
"; # print to html page } } print "
"; } exit(0);