Introduction
 

This   tutorial provides a  handy  resource  for everything  you   need  to   know  to  make  and   keep  cgi   scripts running,using PERL, the World's friendliest language.
 

Cgi   Scripting  is  one  that  is   widely  used  in  all  the  platforms   available under  the sun. The  scope  of  this  tutorial   limits   itself   to  taste  the flavour of Unix and Windows 95/NT alone. 
 

To begin with, I shall  discuss ideas  that are common to both  Unix  and  the  Windows platforms and  later  point out the areas in  which they differ under the  respective 
sections.
 

The introduction is divided into the following sections: 
 
 

               Forms  in HTML

               Form Handling with CGI

               An Example
 
 
 

Forms in HTML

        Forms  in  HTML,  contain  fields whose values are to be
         typed in by the user and submitted to the server side to 
         be manipulated by the Cgi Scripts. 

         Most commonly cited usage of these forms is to   collect
         User feedback and comments about a particular site or a 
         product advertised.
 

       The structure of the form tag is as follows:
 

        < form Name="first" Method="GET" action="/cgi-bin/xyz.cgi">
 

        < /form>

         The 'Name'  attribute of the form is optional  and is used
         to differentiate the different   forms that may be  placed 
         within a single HTML  page.

         The 'Method'  attribute  can  contain a value of  'GET' or
         'POST', the default one being 'GET'. 'GET'  is  rarely used 
         in cases where the form     contents to be submitted  is 
         too  small  in  size. i.e , a Name field and a age field.  In 
         our  discussion  we shall limit ourselves to the      'POST'
         method   which  is the  most  commonly used one.   This
         method   converts   the     fields (form elements)     into 
         'name=value'   pairs  separated  by  an '&' and  transfers
         them.

         The value of  the  'Action'  attribute specifies  which cgi
         script is to be called upon the  submission of this form.
 

         The contents inside the < form> and < /form> could be 
         any valid HTML element. Generally the  following  are used 
         to represent text fields, password fields, buttons, etc. 
 

       Text Input: 

          < input type="text" name='anyname' value='ifany'>

          which shows up on the browser like this 

  
                 
                 

 

       Password Input : 

         < input type="password" name='pass' value='ifrequired'>

           which shows up like this 
 

      
                  
                  

 

       Submit : 

       < input type="Submit" value='Submit Form'> 

            which produces an output like this and submits 
            the form to the server

                      
                   

            Reset  : 
 

             < input type="reset" Value="Clear">

              which produces an output like this  and resets / clears 
              the form fields 

                      
                   

             These are the basics you need to know to handle 'forms' 
              in  Web pages.
 
 

Form Handling with CGI
 

         The work of the CGI Scripts start once the  form elements 
             reach the server. The invoked cgi scripts receive the form 
             elements as environment variables.

             The   form  elements   are  rendered   in   the   form    of 
              'elementname=elementvalue'  which  are  to   be split 
             to make the data received,usable. You can yourself write 
             lines of  Perl code to achieve the task ( Finding out which
             method  of submission  has  been  used,  either    'GET' or 
             'POST'.    If   'GET'   is  used  then  reading the submitted 
             elements   by  using   $ENV{'QUERY_STRING'} or else if 
             'POST'  is  used  reading   STDIN-  The Standard Input to 
             the extent of  $ENV{'CONTENT-LENGTH'},   splitting all 
             the 'name=value' pairs demarcated by '&'   Symbol,    and 
             further splitting the 'names' separately   from the 'values' 
             delimited by the '=' symbol.... Whew!) or    might  as well 
             settle for an easier and effective  solution,    'the cgi-lib' 
             module. 
 

          'cgi-lib.pl'

            This    module    written    by    Mr.  Steven.E.Brenner   is 
             downloadable from  http://cgi-lib.stanford.edu/cgi-lib/  and 
             is a handy tool     that  comes to the  rescue  of  the  Cgi 
             programmers.  The subroutine   'ReadParse'  receives   the 
             submitted    form  elements  and  does  all  the  necessary 
             manipulations and  finally renders  the form elements as 
             an  associative  array  named 'IN'.
 

Let us now see an example, of how the tasks  about which 
             we have discussed until now gets  accomplished.
 
 

          Client Side
 
 

               Customer Detail's Collection Form


 

                                     Enter the Name : 
 
 

                                     Enter your age : 
 
 

                         
                         

 

             This is how the HTML form looks at the client side, 
             the code for which is as follows: 
 
 

              < html> 

              < head>

              < title> Customer details collection form< /title>

              < /head>

              < body> 

              < FORM Name="FirstTrial" METHOD="POST" 

                               Action= "cgi-bin/demo.cgi">
 

              Enter the Name :  < input type="text"  name="name">
 

              Enter your age :  < input type="text" name="age">
 
 

                                      < input type="Submit"> 
 

               < /FORM>

               < /body>

               < /html> 
 

                 Server Side 
 

                At the server side this data reaches in the  following 
                format 
 

                " name=The+name+you+typed&age=31 "
 

                 Notice how the spaces are transformed into '+' Symbols.
                 This data is meaningfully converted using the following 
                 Perl Code with the help of the 'cgi-lib' module , we 
                 discussed earlier.

                                     1. require "cgi-lib.pl"; 
                                     2. &ReadParse; 
                                     3. $nameoftheemployee=$in{'name'};
                                     4. $age=$in{'age'};
 

                  Line 1 : Includes the "cgi-lib.pl" module into the perl 
                             code.

                  Line 2 : Calls the subroutine ' ReadParse '. 

                  Line 3 : The value of the variable 'Name' is stored in the 
                             scalar variable '$nameoftheemployee'.

                  Line 4 : The value of the variable 'Age' is stored in the 
                             scalar variable '$age'.
 

            This  brings  us  to  the   end  of  the   introductory  section.
           Let  us   now  proceed  to  find out how the data submitted 
           gets  stored   and  retrieved   in  a   database    under  the 
           Windows platform.

Back                                          Home                                       Next