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

Intermediate Tutorial - Step 7: Multiple Submits Print

<< Input Validation 2 | JavaScript Actions 2 >>

Multiple "Submit" buttons are a handy feature that can really simplify an interface. For example, you may be writing a form that allows people to add and remove records from a database. Rather than write multiple apps, it is often easier for everyone if a single screen shows the item, along with "Update", "Delete", and "Cancel" buttons along the bottom.

The problem is, how do you tell which one the user clicked? Unfortunately this is problematic, because browsers suck.

FormBuilder works around this problem with some JavaScript magicTM. This allows you to reliably tell which button the person clicked on. Getting to this information is easy - it's returned from the same submitted() function we saw early on.

To create a form with multiple submit buttons, all you have to do is specify an arrayref to the submit option. You may also want to turn off the reset button to prevent confusion:

$form = CGI::FormBuilder->new(
             method => 'post',
             fields => \@fields,
             submit => [qw/Update Delete Cancel/],
             reset  => 0,         # turn off reset button
        );
Getting to the submission mode is easy, just use submitted():
if ($form->submitted eq 'Update' && $form->validate) {
    # code to update record
 
} elsif ($form->submitted eq 'Delete') {
    # code to delete record

} elsif ($form->submitted eq 'Cancel') {
    # do nothing

} else {
    print $form->render(header => 1);
}
There you have a single-screen application that handles the whole process. Note that we only use validate() against the "Update" submit mode, for reasons explained below.

If you were to write an application like this and were using input validation, you would notice one annoying thing. Since FormBuilder has no idea what your different submit buttons are supposed to do, it will generate JavaScript that will try to validate your form fields on every submission. However, you really don't want to have to enter valid data just to "Cancel" and do nothing. Keep reading...

<< Input Validation 2 | JavaScript Actions 2 >>

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