#!/usr/bin/perl ############################################################################### # formRedr: Process an HTML form and redirect to another HTML page. # # 1. Mail the content of the form. # 2. Save the content of the form in a file. # 3. Redirect to another html page. ############################################################################### $addr = 'sam.sultan@nyu.edu'; # setup recipient email address $file = '/home/s/sultans/data/save.file'; # setup name of file $html = '/~sultans/demo/forms/thankyou.html'; # setup a redirect html page use CGI "param"; # use CGI param method @elementNames = param(); # get all FORM element names &mailData(); # Call mailData to e-mail &saveData(); # Call saveData to save data &redirect(); # Call redirect to send html page ############################################################################### # mailData: Mail content of the form fields ############################################################################### sub mailData { open(MAIL, "| /usr/sbin/sendmail $addr "); #pipe to unix sendmail 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 print MAIL "\n $name: "; # send name to mail cmd @values = param($name); # get the values of field foreach $value (@values) { # for each value print MAIL "$value "; # send value to mail cmd } } close(MAIL); } ############################################################################### # saveData: Save HTML form data in a file ############################################################################### sub saveData { open (OUTPUT, ">> $file"); foreach $name (@elementNames) { #for each for field print OUTPUT "\n $name = "; #save the name of field @values = param($name); #get the values of field foreach $value (@values) { #for each value $value =~ s/\r\n/ /g; #substitute newlines with spaces print OUTPUT "$value "; #save the value to file } } print OUTPUT "\n ---- END ----"; close(OUTPUT); } ############################################################################### # redirect: Redirect to another html page ############################################################################### sub redirect { print "Content-type: text/html \n"; print "Location: $html \n"; # create HTTP Location header print "\n"; }