Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML forms and cgi

Status
Not open for further replies.

mossv2

MIS
Mar 4, 2010
5
US
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.

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
 
Hello,

When you press "Append" your script is calling the output_form subroutine which only prints the form. It isn't actually appending anything.

When you press "Finished" it is submitting the form and the state/name that was currently provided is printed.

You must also understand that when a form is submitted, all previous param data is lost and only the latest data is submitted. You may already understand this however your script doesn't incorporate this fact.

You need a method to save every entry, then when finished is pressed to call all the saved entries and print them. There are a number of methods you could do this:

- Using a database.
- Using sessions.
- Consistantly append to a string of entries which can be sent via cgi param.

This post is very similar to a recent post ( and i'd advise that you possibly have a look at the script I posted which uses sessions to pretty much do what you require.

If you need any help modifying your script ill be happy to help, however i'm unsure at this time which method would be most appropriate for you.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top