⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fig15_25.pl

📁 PERL语言资料 可以用于PERL程序设计
💻 PL
字号:
#!/usr/bin/perl
# Fig. 15.25: fig15_25.pl
# Using a MySQL database

use warnings;
use strict;
use DBI;
use DBD::mysql;
use CGI qw( :standard );

print( header(), start_html( "Registration Form" ),
       h1( "Registration Form" ) );

if ( param( "No" ) || !param ) {
   registrationForm();
}
elsif ( !param( "Yes" ) ) {
   my $first = param( "FIRST" );
   my $last  = param( "LAST" );
   my $email = param( "EMAIL" );
   my $phone = param( "PHONE" );
   my $land  = param( "CONTINENT" );
   my $os    = param( "OS" );
   my $time  = param( "HOURS" );
   my $value = param( "RATING" );
   
   if ( $phone !~ / \( \d{3} \) \d{3} - \d{4} /x ) {
      print( "Please enter your phone number ", 
             "in the correct format.", br() );
      registrationForm();
   }
   elsif ( ( $time !~ / \d+ /x ) || $time < 0 || $time > 24 ) {
      print( "Please enter an integer for hours that is ",
             "between 0 and 24", br() );
      registrationForm();
   }
   else {
      print( h4( "You entered", br(),
                 "Name:                $first $last", br(),
                 "E-mail:              $email", br(),
                 "Phone:               $phone", br(),
                 "Continent:           $land", br(),
                 "OS:                  $os", br(),
                 "Hours using product: $time", br(),
                 "Rating of product:   $value:" ), br(),
         
         start_form(),
         hidden( -name => "FIRST" ), hidden( -name => "LAST" ), 
         hidden( -name => "EMAIL" ), hidden( -name => "PHONE" ),
         hidden( -name => "CONTINENT" ), hidden( -name => "OS" ),
         hidden( -name => "HOURS" ), hidden( -name => "RATING" ),
         
         "Is this information correct? ", br(),
         submit( -name => "Yes" ), submit( -name => "No" ),
         end_form() );
   }
}
else {
   my $first = param( "FIRST" );
   my $last  = param( "LAST" );
   my $email = param( "EMAIL" );
   my $phone = param( "PHONE" );
   my $land  = param( "CONTINENT" );
   my $os    = param( "OS" );
   my $time  = param( "HOURS" );
   my $value = param( "RATING" );

   if ( $phone =~ / \( \d{3} \) \d{3} - \d{4} /x ) {
      my $dbh = DBI->connect( "DBI:mysql:USERDB", "root", "",
                              { RaiseError => 1 } );
      
      my $statement = "INSERT INTO Users VALUES
                  ( '$first', '$last', '$email', '$phone',
                     '$land', '$os', '$time', '$value' )"; 
      
      $dbh->do( $statement );

      print( "Thank you for completing the ",
             "registration form $first", br(),
             "The following information has been recorded:", 
             br(), br(), 
             table( { -border => 3, -cellspacing => 3 },
             Tr( th( [ "Name", "E-mail", "Phone Number", 
                       "Continent", "OS", "Hours", 
                       "Rating" ] )),
             Tr( td( { -align => "center" }, 
                     [ "$first $last", $email, $phone, 
                       $land, $os, $time, $value ] ) ) ) );
   }
   else {
      print( "Please enter your phone number in the ",
             " correct format.", br() );
      registration_form();  
   }
}
         
print end_html();
          
sub registrationForm {
   print( 
      h3( "Please fill in all fields and then click Proceed." ),
      start_form(),
      table( { -cellpadding => "3" },
      Tr( { -valign => "top" },
         td( { -width => '300' }, strong( "First Name:" ), br(),
             textfield( -name => "FIRST", -size => 15 ) ),
         
         td( strong( "Last Name:" ), br(),
             textfield( -name => "LAST", -size => 15 ) ) ),
       
      Tr( { -valign => "top" },
          td( strong( "E-mail Address:" ), br(),
              textfield( -name => "EMAIL", -size => 25 ) ),
        
          td( strong( "Phone Number" ), br(),
              textfield( -name => "PHONE", -size => 20 ), br(),
              "Must be of the form (555)555-5555" ) ) ),
         
      h4( "What Continent do you live on? " ),
      popup_menu( -name => "CONTINENT", 
                  -value => [ "North America", "South America",
                              "Asia", "Europe", "Australia", 
                              "Africa", "Antarctica" ] ), 
      br(),          
      h4( "Which Operating System are you currently running?" ),
      radio_group( -name  => 'OS', 
                   -value => [ "Windows 98", "Windows NT", 
                               "Macintosh", "Linux", "Other" ] ),
      br(),
      h4( "How many hours a day do you use our product? " ,
      textfield( -name => 'HOURS', -size => 3 ) ),
         
      h4( "How would you rate our product on a scale of 1 - 5" ),
      radio_group( -name => "RATING",
                   -value => [ '1', '2', '3', '4', '5' ] ),
      br(),             
      submit( "Proceed" ),
      end_form() );
}

###########################################################################
#  (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     #
#  All Rights Reserved.                                                   #
#                                                                         #
#  DISCLAIMER: The authors and publisher of this book have used their     #
#  best efforts in preparing the book. These efforts include the          #
#  development, research, and testing of the theories and programs        #
#  to determine their effectiveness. The authors and publisher make       #
#  no warranty of any kind, expressed or implied, with regard to these    #
#  programs or to the documentation contained in these books. The authors #
#  and publisher shall not be liable in any event for incidental or       #
#  consequential damages in connection with, or arising out of, the       #
#  furnishing, performance, or use of these programs.                     #
###########################################################################

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -