Perl Scripts for CGI

Home Page

You can use these scripts in any way you want and welcome to send questions/suggestions. I am in no way responsible for any problems you come across using these scripts. The module object usage may need some path modifications to work under the present builds of Perl. Please understand that these scripts were written to work under older builds of Perl.

This example shows the HTML code for a POST method front-end, how to read the User's input and parse it, use the ODBC connectivity to fetch the data from the database and then display the data in a dynamically generated table. I did not explain the code in detail since this code is very simple and self-explanatory.

This Database contains a table with Machine name, IP address, location and other details. The input expects the first few letters of the machine name and then the CGI fetches the data and displays the HTML table.

Download some basic shopping card cgi code. Click here to access the demo page. This is only an example. I am not selling any stuff.

HTML code for a single text box POST method

<!Starts Here>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>New Page 1</title>
</head>

<body>

<p>&nbsp;</p>

<blockquote>
<blockquote>
<p align="center"><font color="#008080">Enter the the first few letters or full Machine
Name <br>
</font><strong><font color="#C0C0C0">for the IP address and other details</font></strong></p>
</blockquote>
</blockquote>

<p>&nbsp;</p>

<form method="POST" action="/cgi-bin/ipretrieve.PL">
<p>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="T1" size="20"><input type="submit" value="submit" name="B1">

<input type="reset" value="Reset" name="B2"></p>
<hr>
<p><br>
</p>
<p><font SIZE="2">&nbsp;&nbsp;&nbsp; <a HREF="/nse/systems.html">Previous Page</a></font> </p>
</form>
</body>
</html>

<!Ends Here>

Reading from the POST method

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@array=split(/&/, $buffer);

foreach $array(@array)
    {
    ($name,$value)=split (/=/, $array);
    $FORM{$name}=$value;
    }

Database Connectivity

#Installed the data base server on the web server itself and established a SQL connection.

use Win32::ODBC;
$db = new Win32::ODBC("dsn=sqlname;UID=IUSR_XXXX;PWD=password");
die qq(Not able to connect to the Database. Contact the web master\n) if ! $db;

read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@array=split(/&/, $buffer);
foreach $array(@array)
    {
    ($name,$value)=split (/=/, $array);
    $FORM{$name}=$value;
    }
if ($FORM{'T1'} ne ""){
$myinput="$FORM{'T1'}%";
$db->Sql("SELECT * FROM ip where Machine_Name like '"."\U$myinput\E'");
$db->FetchRow();
@values = $db->Data;

# WHILE Loop starts
    while (@values[0] ne ""){
    $FORM{$counter}=@values[0].@values[1];

# Loop to be completed for successful run

Complete Perl CGI for dynamic page display

print <<EOM;

<html>

<head>
<title>Machine Info</title>
</head>
<body bgcolor="#FFFFFF">

<p align="center"><font color="#000080"><strong><u>IP Look-up Table</u></strong></font></p>
<div align="center"><div align="center"><center>

<table border="4" width="583" cellpadding="6" bgcolor="#008080" bordercolor="#000080">
<tr>
<td width="96" style="font-family: Fixedsys"><font color="#000080">Machine Name</font></td>
<td width="155" style="font-family: Fixedsys"><font color="#000080">Machine Type</font></td>
<td width="90" style="font-family: Fixedsys"><font color="#000080">IP Address</font></td>
<td width="69" style="font-family: Fixedsys"><font color="#000080">Location</font></td>
<td width="80" style="font-family: Fixedsys"><font color="#000080">IN/OUT of Warranty</font></td>
</tr>
</table>
</center></div>
<p align="center">

EOM

use Win32::ODBC;
$db = new Win32::ODBC("dsn=sai;UID=IUSR_ARSINNT;PWD=trustme");
die qq(Not able to connect to the Database. Contact the web master\n) if ! $db;
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@array=split(/&/, $buffer);
foreach $array(@array)
    {
    ($name,$value)=split (/=/, $array);
    $FORM{$name}=$value;
    }
if ($FORM{'T1'} ne ""){
$myinput="$FORM{'T1'}%";
$db->Sql("SELECT * FROM ip where Machine_Name like '"."\U$myinput\E'");
$db->FetchRow();
@values = $db->Data;
# WHILE Loop starts
    while (@values[0] ne ""){
    $FORM{$counter}=@values[0].@values[1];
   
#******
print <<EOM;

<div align="center"><center>
        <table border="4" width="583" cellpadding="6" bgcolor="#FFFFFF" bordercolor="#000080">
        <tr>
<td width="96" style="font-family: Fixedsys"><font color="#000080">
@values[0]</font></td>
<td width="155" style="font-family: Fixedsys"><font color="#000080">
@values[1]</font></td>
<td width="90" style="font-family: Fixedsys"><font color="#000080">
@values[2]</font></td>
<td width="67" style="font-family: Fixedsys"><font color="#000080">
@values[3]</font></td>
<td width="79" style="font-family: Fixedsys"><font color="#000080">
@values[4]</font></td>
</tr>
</table>
</center></div>
<p align="center">
<p><br><br><br>
</p>

EOM

#******
$db->FetchRow();
    @values = $db->Data;
    } # WHILE Loop Ends

    }
else     {
print <<EOM;

<font color="#000080">Please type in the details and</font>
    $myinput
    }
<p align="center"><font SIZE="2"><a HREF="/nse/ipretrieve.html">Try another</a></font> </p>
<p><br>
</p>
<hr align="center">
<p align="center"><br>
</p>
<p align="left"><font SIZE="2"><a HREF="/nse/ipretrieve.html">Previous Page</a></font> </p>
</div>
</p>
</body>
</html>

EOM

 


Home Page