eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

introduction to web programming
self-referential scripts  
When we built the form processor, we saw that we could use one CGI script to reference another by outputting an HTML form. This is pretty powerful since in theory, you could have a web site with no HTML files, but only CGI scripts.

However, in many CGI applications, it is more common for a CGI script to contain routines to display AND process a FORM. This is caled a self-referential script because it outputs a form whose FORM tag points back to itself.

In order to determine what it should do (output a form or process a form) the script needs some extra logic.

The most common way for a CGI script to decide what it should do is to look at the state of the submit button.

The idea is this. If no submit button has been pressed, we can assum that the script is being executed for th first time. hus, we shuold output the FORM. If a submit button has a value, that means that the user has already received the form and is now submitting it for processing.

Consider the following self-referential script called self-refer.cgi:

#!/usr/local/bin/perl
require "cgi-lib.pl";
&ReadParse(*form_data);
print "Content-type: text/html\n\n";

if ($form_data{'submit'} eq "")
  {
  print qq!
  <HTML>
  <HEAD>
  <TITLE>Testing Form Input</TITLE>
  </HEAD>
  <BODY>
  <FORM METHOD = "POST" ACTION = "self-refer.cgi">
  <CENTER>
  <TABLE BORDER = "1">
  <TR>
  <TH>First Name</TH>
  <TD><INPUT TYPE = "text"
                NAME = "f_name"></TD>
  </TR>

  <TR>
  <TH>Last Name</TH>
  <TD><INPUT TYPE = "text"
                NAME = "l_name"></TD>
  </TR>

  <TR>
  <TH>Email</TH>
  <TD><INPUT TYPE = "text"
                NAME = "email"></TD>
  </TR>

  </TABLE>
  <P>
  <INPUT TYPE = "SUBMIT" NAME = "submit">
  </CENTER>
  </FORM>
  </BODY>
  </HTML>!;
  exit;
  }

else
  {
  print qq!
  <HTML>
  <HEAD>
  <TITLE>Testing Form Input</TITLE>
  </HEAD>
  <BODY>
  <TABLE>!;

  foreach $key (keys(%form_data))
    {
    print qq!
    <TR>
    <TD>$key</TD>
    <TD>$form_data{$key}</TD>
    </TR>!;
    }

  print qq!
  </TABLE>
  </BODY>
  </HTML>!;
  }

Previous | Next | Table of Contents