Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Form Processing and Business Logic - Perl

Chapter: Internet & World Wide Web HOW TO PROGRAM - Perl and CGI (Common Gateway Interface)

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Form Processing and Business Logic - Perl

XHTML forms enable Web pages to collect data from users and send the data to a Web server for processing by server-side programs and scripts.

Form Processing and Business Logic

 

XHTML forms enable Web pages to collect data from users and send the data to a Web server for processing by server-side programs and scripts. This allows users to purchase products, send and receive Web-based e-mail, participate in a poll, perform online paging or engage in a number of other tasks. This type of Web communication allows users to in-teract with the server and is vital to Web development.

 

Figure 27.12 uses an XHTML form to collect information about users before adding them to a mailing list. This type of registration form could be used, by a software company to obtain profile information for a user’s company database before allowing the user to down-load software.

 

Line 21 contains a form element which indicates that, when the user clicks Register, the form information is posted to the server. The statement action = "cgi-bin/ fig27_13.pl" directs the server to execute the fig27_13.pl Perl script (located in the cgi-bin directory) to process the posted form data. We assign a unique name (e.g., email on line 33) to each of the form’s input fields. When Register is clicked, each field’s name and value is sent to the script fig27_13.pl, which can then access the submitted value for each specific field.

 

 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   

    <!-- Fig. 27.12: fig27_12.html -->

 

    <html>

    <head>

    <title>Sample form to take user input in XHTML</title>

    </head>

   

    <body style = "font-face: arial; font-size: 12pt">

   

 

    <div style = "font-size: 14pt; font-weight: bold">

    This is a sample registration form.

    </div>

   

    <br />

    Please fill in all fields and click Register.

   

    <form method = "post" action = "/cgi-bin/fig27_13.pl">

   

    <img src = "images/user.gif" /><br />

   

    <div style = "color: blue" >

    Please fill out the fields below.<br />

    </div>

   

    <img src = "images/fname.gif" />

    <input type = "text" name = "fname" /><br />

    <img src = "images/lname.gif" />

    <input type = "text" name = "lname" /><br />

    <img src = "images/email.gif" />

    <input type = "text" name = "email" /><br />

    <img src = "images/phone.gif" />

    <input type = "text" name = "phone" /><br />

   

    <div style = "font-size: 10pt">

    Must be in the form (555)555-5555.<br /><br />

    </div>

   

    <img src = "images/downloads.gif" /><br />

    <div style = "color: blue">

    Which book would you like information about?<br />

    </div>

   

    <select name = "book">

    <option>Internet and WWW How to Program 2e</option>

    <option>C++ How to Program 3e</option>

    <option>Java How to Program 4e</option>

    <option>XML How to Program 1e</option>

    </select><br /><br />

   

    <img src = "images/os.gif" /><br />

<div style = "color: blue">

Which operating system are you currently using?

</div><br />

 

<input type = "radio" name = "os"

value = "Windows NT" checked />

Windows NT<input type = "radio"

name = "os" value = "Windows 2000" />

Windows 2000<input type = "radio"

name = "os" value = "Windows 98/me" />

Windows 98/me<br /><input type = "radio"

name = "os" value = "Linux" />

Linux<input type = "radio" name = "os"

value = "Other" />

Other<br /><input type = "submit"

value = "Register" />

    </form>

    </body>

    </html>


 

Fig. 27.12  XHTML document with an interactive form

 

The program in Fig. 27.13 processes the data posted by fig27_12.html and sends a Web page response back to the client. Function param (lines 8–13) is part of the Perl CGI module and retrieves values from a form field’s value. For example, in line 35 of the previous figure (Fig. 27.12), an XHTML form text field is created with the name phone. In line 12 of Fig. 27.13, we access the value that the user entered for that field by calling param( "phone" ) and assign the value returned to variable $phone.

 

In line 24, we determine whether the phone number entered by the user is valid. In this case, (555)555-5555 is the only acceptable format. Validating information is crucial when you are maintaining a database, and a great way to do this is using regular expressions. Vali-dation ensures that data is stored in the proper format in a database, that credit-card numbers contain the proper number of digits before encrypting them for submission to a merchant, etc. The design of verifying information is called business logic, or business rules.

 

The if condition in line 24 uses a regular expression to validate the phone number. The expression “\(” matches the opening parenthesis of the phone number. Because we want to match the literal character (, we must escape its normal meaning by using the \ character. This sequence must be followed by three digits (\d{3}), a closing parenthesis, three digits, a hyphen and finally, four more digits. Note that we use the ^ and $ metachar-acters to ensure that there are no extra characters at the beginning or end of the string.

 

If the regular expression is matched, then the phone number is valid, and a Web page is sent, thanking the user for completing the form. If the user posts an invalid phone number, the else (lines 63–76) is executed, instructing the user to enter a valid phone number.

 

    #!/usr/bin/perl

    # Fig. 27.13: fig27_13.pl

    # Program to read information sent to the server

    # from the form in the fig27_12.html document.

    use CGI qw( :standard );

 

    $os = param( "os" );

    $firstName = param( "fname" );

      $lastName = param( "lname" );

      $email = param( "email" );

      $phone = param( "phone" );

      $book = param( "book" );

      $dtd =

      "-//W3C//DTD XHTML 1.0 Transitional//EN\"

      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";

      print(         header() );     

                       

      print(         start_html(     { dtd => $dtd,

                        title => "Form Results" } ) );

                       

      if ( $phone =~ / ^ \( \d{3} \) \d{3} - \d{4} $ /x ) {

      print( "Hi " );

      print( span( { style => "color: blue; font-weight: bold" },

      $firstName ) );

      print( "!" );

 

 

      print( "\nThank you for completing the survey." );

      print( br(), "You have been added to the " );

           

      print( span( {       style => "color: blue; font-weight: bold" },

            $book ) );

      print( " mailing list.", br(), br() );

           

      print( span( {       style => "font-weight: bold" },

      "The following information has

            been saved in our database: " ), br() );

           

      print( table(         

      Tr( th(        { style => "background-color: #ee82ee" },

            "Name" ),

      th( { style => "background-color: #9370db" },

            "E-mail" ),

      th( { style => "background-color: #4169e1" },

            "Phone" ),

      th( { style => "background-color: #40e0d0" },

            "OS" ) ),

           

      Tr( { style => "background-color: #c0c0c0" },

      td( "$firstName $lastName" ),

      td(  $email ),

      td(  $phone ),

      td(  $os ) ) ) );

           

      print( br() );          

           

      print( div( { style => "font-size: x-small" },

      "This         is only a sample form. You have not been

      added to a mailing list." ) );

      }

      else {

      print( div( { style => "color: red; font-size: x-large" },

      "INVALID PHONE NUMBER" ), br() );

     

      print( "A valid phone number must be in the form " );

      print( span( { style => "font-weight: bold" },

      "(555)555-5555." ) );

     

      print( div( { style => "color: blue" },

      "Click the Back button, and enter a

      valid phone number and resubmit." ) );

      print( br(), br() );

      print( "Thank you." );

      }

 

      print( end_html() );

 



 

Fig. 27.13  Script to process user data from fig27_12.html

 

The br function (line 31) adds a break (<br />) to the XHTML page, while methods span (line 26) and div (line 59) add <span> and <div> tags to the page, respectively.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.