CGI Scripting methodology, the ‘Application Mode’s Approach’

 

People new to cgi programming sometimes like to have each piece of code that processes form input in a separate file on their server.  I think this is redundant and see good programmers not typically doing this.  Some other programmers like to base their programs logic on the names of html submit buttons.  I think this is not a good idea either, because these values can change.

 

My solution is something I like to call the ‘Application Mode’s Approach’.

 

Let’s say you have a script that does 3 things.

  • it prints an html form
  • it records data from the form into the database
  • it lets you look at the data that’s been entered

 

I would create this application in the following manner, using a name value pair with a name of ‘mode’ to determine what the script should do on each request. 

 

Note: when using CGI.pm, it will automatically fill in the values of form fields in generated html pages if you use it’s HTML writing capability.  However, sometimes you don’t want it to do that, so you pass it an an argument of override=>1.

 

Consider the following code:

 

#!/usr/bin/perl –w

 

use strict;

use CGI::Carp qw(fatalsToBrowser);

use CGI qw(:standard);

use DBI; # we’ll talk about this in more detail later

 

my $mode = param(‘mode’);

my $url = url;

 

print header,

start_html;

if($mode eq ‘’) # two open quotes means ‘blank’ or ‘null’ or ‘empty’ in perl

{

  print h2(‘Please enter your name’),

  start_form(-action=>$url,

             -method=>’post’),

  hidden(-name=>’mode’,

              -value=>’process_form’,

              -override=>1),

  textfield(‘name’),

  end_form;

}

elsif($mode eq ‘process_form’)

{

  my $sth = $dbh->prepare(‘insert into names (name) values (?)’);

  $sth->execute(param(‘name’));

  print redirect($url . ‘?mode=read_submitted_names’);

}

elsif($mode eq ‘read_submitted_names’)

{

  my $sth = $dbh->prepare(‘select * from names’);

  while(my $row_href = $sth->fetchrow_hashref)

  {

    print $row_href->{‘name’} . br;

  }

  print h2(‘Bad mode specified.  Exiting…’);

}

print end_html;

 

This code allows us to print an html form, store the results in a database, and read the results out of the database.  This is a good example of the ‘Application Mode’s Approach’.

 


<-- Previous: Using CPAN modules to save coding time/complexity, namely CGI.pm | Next: Basic Relational Database Concepts & Design, The SQL Language, And The mySQL Database Server -->


Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:


Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows


Link To
This Page!


Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script