Perl Form Builder ::Features ::Download ::Tutorial ::Examples
::Documentation ::FormBuilder Google Group ::Old List Archives ::Contribute
Note: The mailing list is has been replaced by the Google FormBuilder Group

Basic

1. Intro to FormBuilder
2. A Simple Form
3. Form Method
4. Input Validation
5. Field Options
6. Multi-Select Lists
7. Field Data
8. Field Attributes
9. HTML Formatting
10. Custom Headers
S. Summary

Intermediate

1. Trickier Forms
2. Perl References
3. Default Values
4. CGI Stickiness
5. JavaScript Actions
6. Input Validation 2
7. Multiple Submits
8. JavaScript Actions 2
9. Modules & mod_perl
10. Smartness
S. Summary

Advanced

1. Remaining Features
2. Intro to Templates
3. HTML::Template
4. HTML::Template 2
5. Template Toolkit
6. Exploiting render()
7. Multi-Screen Apps
8. Multi-Screen Apps 2
9. Multi-Screen Apps 3
10. Custom Messages
S. Summary

Advanced Tutorial - Step 9: Multi-Screen Apps 3 Print

<< Multi-Screen Apps 2 | Custom Messages >>

Now onto the other alternative:

  1. Create a single $form object, and get to the other params with $form->cgi_param
This is useful if you are writing a component that needs to do a couple different things, such as add, edit, and delete all in one. For example, it is usually easier to write a single component that does these three things, even if you are using the multi-component methodology.

Here is an example that combines just the add, edit, and delete functions for an online calendar. The display, search, and other functions would be in separate components:

#!/usr/bin/perl

# editappt.pl - add, edit, or delete appointments in the calendar

use strict;       # recommended but not required

use CGI::FormBuilder;

# create form at top
my $form = CGI::FormBuilder->new(
                fields => [qw/apptid apptname username location comments/],
                header => 1,            # always send header
                keepextras => 1         # keep action param
           );

$form->field(name => 'apptid', static => 1);        # can't edit appt

my $action = $form->cgi_param('action') || 'add';   # what to do

if ($action eq 'add') {
    # Add new appt
    if ($form->submitted && $form->validate) {
        if ($form->submitted eq 'Cancel') {
            # do nothing
            print $form->confirm(text => "Nevermind, canceled request\n");
        } else {
            # create new appt
        }
    } else {
        # Print add form
        print $form->render(submit => [qw/Add Cancel/]);
    }

} elsif ($action eq 'edit') {
    # Edit or delete appt
    if ($form->submitted && $form->validate) {
        if ($form->submitted eq 'Cancel') {
            # do nothing
            print $form->confirm(text => "Nevermind, canceled request\n");
        } elsif ($form->submitted eq 'Delete') {
            # delete appt
        } else {
            # update appt
        }
    } else {
        # Editing, so should have specified an apptid
        my $apptid = $form->field('apptid') || die "No apptid for edit";
        
        # Do a database select
        my $apptref = $sth->selectrow_hashref("select * from db
                                               where apptid = '$apptid'");

        # Print edit form
        print $form->render(submit => [qw/Update Delete Cancel/],
                            values   => $apptref);
    }
}
As you can see, the same form is used for both screens. The only differences are (a) the edit screen is pre-filled with default values and (b) the add and edit screens have different buttons at the bottom. Notice how we exploited options to render() to make this happen.

<< Multi-Screen Apps 2 | Custom Messages >>

FormBuilder is © Nate Wiger, with contributions from many people.
Nateware