Sample <FORM> based CGI Program

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

This program accepts input from a <FORM> based HTML document and creates an on the fly HTML document. The HTML document is as follows:

<html>
<head>
<title><Form Example</title>
</head>
<body>
<h2>Please fill in your name</h2>
<form action="http://www.someplace.com/cgi-bin/reply.cgi" METHOD=POST>
<INPUT TYPE=TEXT NAME="name" MAXILENGTH=40>
<INPUT TYPE=SUBMIT>
</form>
</body>
</html>

Each CGI program is designed to handle a specific query submitted by the user by filling a form.

However, all the data requires similar processing to extract the fields and their values. For this you can use a common set of sub-routines. As you might expect for a popular tool such as Perl, there is already such a Perl library, and that common library happens to be cgi-lib.pl by Steven Brenner. cgi-lib.pI is the accepted library for parsing the CGI query strings that the web server sends to

the CGI program. By using the cgi lib.pl library we can quickly create Perl programs to handle form input.

The cgi lib.pl library works with both Perl 4 and Perl 5. If your ISP has Perl 5 installed, you may want to use the CGI module, an object oriented CGI processing module that works in Perl 5 only.

Because cgi lib.pl is a library of Perl subroutines in a text file, there is nothing much to install. All you need to do is get the latest version of the file and place it in the /cgi bin/ directory of your web server.

#reply.cgi
$|=1
require "cgi-lib.pl";
print &PrintHeader;
print &HTMLTop("On the fly HTML");
if(&REadParse("Input"))
{
#value of the 'name' field (text field) entered in HTML
#document
$value=$input{"name"};
print "<H!>Thank you $value</H1>";;
}
else
{
print"<H1>Error reading from input</H1>";
}
print &HTMLBot;

The CGI.pm module, developed by Lincoln D.Stein, uses Perl 5's object oriented style of programming. You need Perl version 5.001 or higher to use the CGI module; Perl 5.003 is the recommended version.

Like cgi lib.pl library, CGI.pm enables you to accept a query (submitted through an HTML form) and easily extract the parameters submitted by the user, Unlike cgi lib.pl with its procedural interface, CGI pm provides its functionality through a CGI object.

To use the CGI.pm module, you begin by inserting the following statement in your Perl script:

use CG I;

After that, create a CGI object as follows.

$a= new CGI;

The creation of CGI object involves parsing the CGI query string and extracting all environment variables relevant to CGI programming. $a, will reference to the new CGI object. From then on you can access the methods of the CGI object through the $a reference.

Parsing a CGI query is the only one part of the CGI module, and the parsing occurs when you create the CGI object. Additionally, the CGI object has many more features, such as the following:

methods for creating HTTP headers that you need to include in HTML documents being sent back from the script

methods for creating HTML forms

methods for handling advanced features such as frames, Netscape cookies
and JavaScript

After you create a new CGI object and get a reference to that object, you can begin accessing the parameters in the HTML form's data, To access the parameter by name, call the param method with the name (form element like textbox) as an argument, as follows:

$val = $a->param('name');

where $val gets the value entered by the user.

To send back an HTML document, begin by calling the print function and invoke the CGI object's header method as argument.



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.