#!/usr/bin/perl -wT use strict; use CGI; use CGI::Carp qw( fatalsToBrowser ); my $q = new CGI; my $colon = ":"; my $f_name = ""; my $f_name_error = ""; my $f_email_error = ""; my $f_mail_address = ""; my $f_mail = ""; my $f_mail_error= ""; my $f_city = ""; my $f_city_error = ""; my $f_state = ""; my $f_state_error = ""; my $f_zip = ""; my $f_zip_error = ""; my $f_share = ""; my $f_shareword = ""; my $f_email = ""; my $p_Terrors = 0; my $f_contact = ""; print "Content-type: text/html\n\n"; # If the first time the form is shown, then ... if ($q->param == 0) { &S_Show_Form(); exit(); } else { # If this is not the first time the form is shown. #1. Get the value for the control named f_name. $_ = $q->param("f_name"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_name = $_; $p_Terrors = 0; #3. Check each form field for errors. if ($f_name eq "") { $f_name_error = "Name may not be blank.
\n"; $p_Terrors = $p_Terrors + 1; } $_ = $q->param("f_email"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_email = $_; if ($f_email eq "") { $f_email_error = "Email may not be blank.
\n"; $p_Terrors = $p_Terrors + 1; } $_ = $q->param("f_mail_address"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_mail_address = $_; if ($f_mail_address eq "") { $f_mail_error = "Address may not be blank.
\n"; $p_Terrors = $p_Terrors + 1; } $_ = $q->param("f_city"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_city = $_; if ($f_city eq "") { $f_city_error = "City may not be blank.
\n"; $p_Terrors = $p_Terrors + 1; } $_ = $q->param("f_state"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_state = $_; if ($f_state eq "") { $f_state_error = "State may not be blank.
\n"; $p_Terrors = $p_Terrors + 1; } $_ = $q->param("f_zip"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_zip = $_; unless ( $f_zip =~ /\d{5}/ ) { $f_zip_error = "Zip code cannot be blank and must be 5 digits in length.
\n"; $p_Terrors = $p_Terrors + 1; } #4. When all done checking individual form fields for errors, see if the count of errors = zero. if ($p_Terrors == 0) { #No errors. Call subroutine "S_Process_Form" &S_Process_Form(); } else { #We had one or more errors, Show the form again. &S_Show_Form(); } } #End if $q == 0 exit(); #--------------- sub S_Show_Form { #--------------- print "\n"; print "WEB SITE REGISTRATION\n"; print "\n"; print "\n"; print "\n"; print "
\n"; print "

REGISTER WEB SITE

\n"; print "
\n"; print $q->start_form(-action => "customer.cgi", method => "get"); #The next print statment will print "" (nothing) if # there was no error in registering person's name. # Otherwise it will print the error message. print $f_name_error; print "Name: ", $q->textfield(-name=>"f_name", -size=>30), "
\n"; print $f_email_error; print "E-mail address: ", $q->textfield(-name=>"f_email", -size=>30), "
\n"; print "$f_mail_error"; print "Mailing address: ", $q->textfield(-name=>"f_mail_address", -size=>30), "
\n"; print "$f_city_error"; print "City: " . $q->textfield(-name=>"f_city", -size=>30), "
\n"; print "$f_state_error"; print "State: " . $q->popup_menu(-name=>"f_state", -value=>['Please select your state', 'Az', 'Ca', 'Hi'], -labels=>{'Az'=>'Arizona', 'Ca'=>'California', 'Hi'=>'Hawaii'}), "
\n"; print "$f_zip_error"; print "Zip: ", $q->textfield(-name=>"f_zip", -size => 15), "
\n"; print "Contact: ", $q->radio_group(-name=>"f_contact", -value=>['Email', 'Postal mail'], -default=>'Email'), "
\n"; print "Share with others?", $q->checkbox(-name=>'f_share', -value=>'', -label=>''), "
\n"; #The next two lines are sample for print the email address error (if any) #and the text field for the email address. print $q->submit(-value =>"Submit Information"); print "
\n"; print $q->end_form(); } #end-sub S_Show_Form #----------------- sub S_Process_Form { #----------------- #The test immediately below checks the value #of the "share/don't share" checkbox and sets #a variable named $Vshareword = "Y" or "N" --- #so that value can be written to the customer.txt file. if ($f_share eq "on") { $f_shareword = "Y"; } else { $f_shareword = "N"; } open(CUSTFILE, ">>customer.txt"); print CUSTFILE "$f_name" . "$colon" . "\n"; close(CUSTFILE); print "$f_name thank you for registering at our site.
\n"; if ($f_share eq "on") { print "We appreciate you allowing us to share your information with our sponsoring companies.
\n"; } else { print "We acknowledge your request that your information NOT be shared with others.
\n"; } } #end sub ProcessForm