#!/usr/bin/perl ############################################################################### # procForm: Process an HTML form # # 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 an XML 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 from field return if (! $email); #exit if no email specified # open(MAIL, "| /usr/sbin/sendmail $email "); #pipe to unix sendmail command open(MAIL, "| sendmail $email "); #pipe to unix sendmail command print (MAIL "From: party server \n"); print (MAIL "To: $email \n"); 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 (INPUT, "< $file") or die("Cannot open input file $file - $!"); @array = ; # read file into array close(INPUT); if ($array[0] =~ /stylesheet/) { #if first line is $array[0] = ""; #get rid of $array[1] = ""; #get rid of $array[$array-1] = ""; #get rid of } open (OUTPUT, "> $file") or die("Cannot open output file $file - $!"); print OUTPUT ''; print OUTPUT "\n"; print OUTPUT " \n"; print OUTPUT @array; # write the array print OUTPUT " \n"; foreach $name (@elementNames) { # for each form field if ($name =~ /(email|file|redirect)/i) { # if name is one of these next; # skip it } @values = param($name); # get the values of field if (@values <= 1) { # if single value print OUTPUT " <$name>$values[0] \n"; #print it } else { # if multi values print OUTPUT " <$name> \n"; # print opening tag foreach $value (@values) { # for each value print OUTPUT " \n"; #print it } print OUTPUT " \n"; # print closing tag } } print OUTPUT " \n"; 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);