in reply to Changing the Value Assigned To A Hash Key
This should be enough of the code to let you see the complete picture. Hopefully I didn't miss anything important.
#!perl.exe use strict; use warnings; use CGI qw(:standard -no_xhtml); # the check_name, etc, functions are in this require'd file except in # this example code # require "D:/Inetpub/wwwroot/gpa/functions.pl"; my @errors; my %app; $app{"first_name"} = param('first_name'); $app{"telephone"} = param('telephone'); $app{"email"} = param('email'); # Verify the POST data (if any) using custom tests and undef bad # values. If everything checks OK, accept the data and print a # success message. Else print the application form (which will # preserve all good values which users will appreciate. if (request_method() eq "POST") { my $first_name_check = &check_name($app{"first_name"}); unless ($first_name_check == "1") { undef $app{"first_name"}; push (@errors, "First Name: ".$first_name_check); } my $phone_check = &check_phone($app{"telephone"}); unless ($phone_check == "1") { undef $app{"telephone"}; push (@errors, "Telephone: ".$phone_check); } my $email_check = &check_email($app{"email"}); unless ($email_check == "1") { undef $app{"email"}; push (@errors, "E-mail: ".$email_check); } if (@errors) { # Give the user another chance. &print_application; } else { # Done! &print_accepted; } } else { # Give the user an application form. &print_application; } # We want first and last names to be a certain way. sub check_name { my $name = $_[0]; if ($name =~ /^[A-Z][A-Za-z\-']{2,40}/s) { return 1; } else { return "Helpful message."; } } # Chopped other subs to shorten post sub check_phone { return 1; } sub check_email { return 1; } # Print account application page. sub print_application { # chopped lots of non-relevant code... # here's those helpful error messages regarding BAD data... if (@errors) { print h2("Account Application Error"), "\n\n", p(b("Problems found...")), "\n\n", ol(li[@errors]), "\n\n"; } # print the form and populate form with GOOD data... # Avoiding line wraps in on-line post. n/b the funny indents :-) Tr( [ td([ b('First Name:'), textfield(-name=>'first_name', -default=>"$app{'first_name'}") ]), td([ b('Organization:'), textfield(-name=>'organization', -default=>"$app{'organization'}") ]), td([ b('Telephone:'), textfield(-name=>'telephone', -default=>"$app{'telephone'}") ]), td([ b('E-mail:'), textfield(-name=>'email', -default=>"$app{'email'}") ]), td([ '', submit ]) ] ) } # Print account accepted page. sub print_accepted { # ... }
|
|---|