Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Building a Form Processor

Building a Form Processor

  • Okay, now that we know how to read and parse incoming form data, it is time to put all the pieces together and write or first CGI program.

  • The most basic CGI program is the Form Processor that simply reads incoming form data and processes it. In this section, we will create a form processor to simply grab form data and echo it back to the web.

  • As always, first we will need an HTML form so that the user can submit some data. In this case, let's take advantage of the printing functions in perl and have a CGI script dynamically output he HTML instead of having an purely-HTML-based form. Consider the following script:

    #!/usr/local/bin/perl
    print "Content-type: text/html\n\n";
    print qq!
      <HTML>
      <HEAD>
      <TITLE>Testing Form Input</TITLE>
      </HEAD>
      <BODY>
      <FORM METHOD = "POST" ACTION = "fp.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">
      </CENTER>
      </FORM>
      </BODY>
      </HTML>!;
    

  • As you can see, this script simply outputs an HTML form. On the web, the following CGI script would produce the following interface:

[Example]

  • As you can see, when the user hits the "Submit" button, they will send their information to fp.cgi via the POST method. Let's take a look at fp.cgi:

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

  • fp.cgi will simply read and parse the form data and then echo back the user's input. The following figure shows a typical response:

[Example]

Notice that the form values do not come in the order that you specified on your HTML form. This is because associative arrays store their name/value pairs using their own logic for memory savings. You have to write your own logic for sorting name/value pairs if that is important to you

Additional Resources:

Reading and Parsing Form Data with cgi-lib.pl
Table of Contents
Self-Referential Scripts


Up to => Home / Authoring / Scripting / Tutorial




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers