UB Information Technology

Example: Writing to a tab deliminated file

This is an example of a cgi form written using Perl.

Sample Web Form for Tab-Delimited File Manipulation

This web form accepts 7 input fields (name, address, city, state, zipcode, phone, and email. When the form is filled out and the "Submit" button is clicked it calls the write-tab-delimited.cgi file to store that data.

These files should be used simply as examples. They are not robust enough to simply copy and use without modifications. We'll leave that exercise to you.

What the form looks like:

NOTE: This is just an example. Clicking the "Submit" button will not actually do anything here.

Name:
Address:
City:
State / Province:
Zip / Postal Code:
Phone:
Email Address:

HTML source code for the web form:

<html>
<head>
  <title>Form example</title>
</head>
<body bgcolor="ffffff">

<form method="post" action="write-tab-delimited.cgi">
<table>
  <tr>
    <td align="right">
      <strong>Name:</strong>
    </td>

    <td align="left">
      <input type="text" size="25" name="name">
    </td>
  </tr>
  <tr>
    <td align="right">

      <strong>Address:</strong>
    </td>
    <td align="left">
      <input type="text" size="25" name="address">
    </td>

  </tr>
  <tr>
    <td align="right">
      <strong>City:</strong>
    </td>

    <td align="left">
      <input type="text" size="25" name="city">
    </td>
  </tr>
  <tr>
    <td align="right">

      <strong>State / Province:</strong>
    </td>
    <td align="left">
      <input type="text" size="3" name="state">
    </td>

  </tr>
  <tr>
    <td align="right">
      <strong>Zip / Postal Code:</strong>
    </td>

    <td align="left">
      <input type="text" size="25" name="zipcode">
    </td>
  </tr>
  <tr>
    <td align="right">

      <strong>Phone:</strong>
    </td>
    <td align="left">
      <input type="text" size="25" name="phone">
    </td>

  </tr>
  <tr>
    <td align="right">
      <strong>Email Address:</strong>
    </td>

    <td align="left">
      <input type="text" size="25" name="email">
    </td>
  </tr>
  <tr>
    <td align="right">

      <input type="submit" value="Submit">
    </td>
    <td align="left">
      <input type="reset" value="Reset">
    </td>
  </tr>

</table>
</form>
</body>
</html>

Perl source code for the write-tab-delimited.cgi program

#!/usr/local/bin/perl
# write-tab-delimited.cgi
# Jim Gerland 5/24/1999
# Sample script to write form input to a tab-delimited file

require "/info/www/cgi-bin/cgi-lib.pl";

&ReadParse;

$tabfile = "file.tab";

open(OUT,">>$tabfile");
print OUT "$in{'name'}\t";
print OUT "$in{'address'}\t";
print OUT "$in{'city'}\t";
print OUT "$in{'state'}\t";
print OUT "$in{'zipcode'}\t";
print OUT "$in{'phone'}\t";
print OUT "$in{'email'}\n";
close(OUT);

print &PrintHeader;
print "<html><head><title>Thanks!</title></head>\n";
print "<body bgcolor="#ffffff">\n";
print "<h1 align="center">Thanks\!</h1>\n";
print "<p>Name: $in{'name'}<br>\n";
print "Address: $in{'address'}<br>\n";
print "City: $in{'city'}<br>\n";
print "State / Province: $in{'state'}<br>\n";
print "Zip / Postal Code: $in{'zipcode'}<br>\n";
print "Phone: $in{'phone'}<br>\n";
print "Email: $in{'email'}</p>\n";

exit;