#!/usr/bin/perl
###############################################################################
# formChain: chain forms by adding form elements of first form
#                        as hidden form elements in second form
#
# First form must have a hidden field as:
#        <input type=hidden name=next value='/path/second_form.html'>
###############################################################################
 
use CGI "param";                                  # use CGI "param" module
 
@elementNames = param();                          # get all FORM element names
 
$nextform = param('next') || param('NEXT');       # get next form dir/name

print "Content-type: text/html \n";
print "\n";

open(HTML, "< $nextform") || die("Could not open $nextform - $!");

while($line = <HTML>) {                           # while more lines
    if ($line =~ /<\/form>/i ) {                  # check for </form> line
        foreach $name (@elementNames) { 
            if ($name !~ /next/i) {               # if field name != next 
                @values = param($name);
                foreach $value (@values) {
                    print "<input type=hidden name=$name value=\"$value\" > \n";
                }
            }
        }
    }
    print $line;
}

close(HTML);

exit(0);