The CGI forum doesn't appear to have much activity, so I'm posting this to the Perl forum.
When "Append" is clicked I want to save each record, and when "Finished" is clicked it will output a page with each record that was entered as shown in the above example.
I am not having a problem doing this to print one record, but no matter what I do I cannot get it to print multiple records like I want in my desired output.
Desired output
When "Append" is clicked I want to save each record, and when "Finished" is clicked it will output a page with each record that was entered as shown in the above example.
I am not having a problem doing this to print one record, but no matter what I do I cannot get it to print multiple records like I want in my desired output.
Code:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
sub output_top($);
sub output_end($);
sub display_results($);
sub output_form($);
my $q = new CGI;
print $q->header();
output_top($q);
if ($q->param('Append')) {
#push(@q,$q);
#my @q = param($q);
output_form($q);
}
elsif ($q->param('Finished')) {
# Parameters are defined, therefore the form has been submitted
display_results($q);
} else {
# We're here for the first time, display the form
output_form($q);
}
output_end($q);
exit 0;
sub output_top($) {
my ($q) = @_;
print $q->start_html(
-title => 'Form',
-bgcolor => 'white',
-style => {
-code => '
/* Stylesheet code */
body {
font-family: verdana, sans-serif;
}
h2 {
color: darkblue;
border-bottom: 1pt solid;
width: 100%;
}
div {
text-align: right;
color: steelblue;
border-top: darkblue 1pt solid;
margin-top: 4pt;
}
th {
text-align: right;
padding: 2pt;
vertical-align: top;
}
td {
padding: 2pt;
vertical-align: top;
}
/* End Stylesheet code */
',
},
);
print $q->h2("Form");
}
sub output_end($) {
my ($q) = @_;
print $q->div("My form");
print $q->end_html;
}
sub display_results($) {
my ($q) = @_;
my $username = $q->param('user_name');
my @states = $q->param('state');
print $q->h4("$username");
print $q->table(
{-border => 1, -cellpadding => 3},
$q->Tr($q->td(\@states)),
);
}
sub output_form($) {
my ($q) = @_;
print $q->start_form(
-name => 'main',
-method => 'POST',
);
print $q->start_table;
print $q->Tr(
$q->td('Name:'),
$q->td(
$q->textfield(-name => "user_name", -size => 50)
)
);
my @values = ('AL', 'AK', 'AR', 'MS', 'MI', 'GA', 'FL', 'LA', 'TX', 'KY');
print $q->popup_menu(
-name => 'state',
-values => \@values,
-default => 'value2'
);
print $q->Tr(
#$q->td($q->submit(-value => 'Submit')),
$q->td($q->submit(-name => "Append", -value => 'Append')),
$q->td($q->submit(-name => "Finished", -value => 'Finished')),
$q->td(' ')
);
print $q->end_table;
print $q->end_form;
}
Desired output
Code:
Name State
John VT
Sarah TX
Mike CA
Jen CA