#!/usr/bin/perl -w use strict; use CGI::Safe qw/:standard taint /; # Initial variables my @languages = ('English', 'Francais'); my @colours = ('Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet', 'Black'); my $fileDir = "/home/stef/outputs/"; my $filename = "test.csv"; ################################################################### # Basic Form Config # ################################################################### my $query = new CGI; print $query->header; print $query->start_html("Test Basic Form"); my $step = param('step') || 0; # Subroutines: part1() unless $step; part2() if $step == 2; part3() if $step == 3; print $query->end_html; ################################################################### # Subroutines # ################################################################### sub part1 { print $query->h2("Welcome to this basic form"); print $query->startform; print $query->hidden({-name => 'step', -value => 2, -override => 1}); print $query->popup_menu({-name => 'language', -values => \@languages, -default => \$languages[0], -label => "Please select language"}); print $query->submit({-value => "Continue"}); print $query->endform; } sub part2 { print $query->h2("Part Two"); print $query->startform; print $query->hidden({-name => 'step', -value => 3, -override => 1}); print $query->hidden({-name => 'language'}); print "
Please choose your favourite colour:
"; print $query->radio_group({-name => 'colour', -values => \@colours, -default => \$colours[0], -linebreak => 'true'}); print $query->submit({-value => "Continue"}); print $query->endform; } sub part3 { print $query->h2("Almost Done!"); print $query->startform; print $query->hidden({-name => 'step', -value => 4, -override => 1}); print $query->hidden({-name => 'language'}); print $query->hidden({-name => 'colour'}); print "Press the Submit button to download csv file
"; print $query->submit({-value => "Continue"}); print $query->endform; download($filename) or error('An unknown error has occured.'); } sub download { my $file = $_[0] or return(0); my $path_to_files = "/home/stef/outputs/"; open(my $DLFILE, '<', "$path_to_files/$file") or return(0); print $query->header(-type => 'application/x-download', -attachment => $file, 'Content-length' => -s "$path_to_files/$file", ); binmode $DLFILE; print while <$DLFILE>; undef ($DLFILE); return(1); }