Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populate an HTML form from a file with CGI 1

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
US
I have written a program that uses an HTML form to collect information with radio buttons, drop down lists, and text boxes, then submits it to a cgi script that stores it to a file and / or displays the contents of the file. It would be nice to be able to chooses one of those records (I can handle that part) and put that information BACK into the HTML form so it could be edited if needed. I just need some code samples of how to actually get perl to call that HTML form and pass data BACK to it. For example, the form has a radio button for department - available selections are sales, IT, management. In the file where the info is stored, it would be stored as the word sales (for example). Lets assume I open the file and input the word sales into the perl variable "department" - how do I call the form with the perl cgi and have it select whichever radio button info is stored in the "department" variable? Same idea for text boxes and drop down lists. A couple of brief samples would be appreciated. If I have to have perl completely re-create the form instead of calling it, then a couple of brief examples on how to create the form with the proper selections (example - when it creates the form it selects the sales radio button because that is what was in the "department" variable) would be appreciated.

Thanks!
 
Here's an online order form I built for one of my clients. It uses DBF type databases to retrieve user info. Give me a shout if I can help further: patrick@thevegasidea.com

#!/usr/bin/perl -w

use DBI;
use CGI;
use CGI qw(param);
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use Fcntl ":flock";

my $acct = param("acct");
my $logdate = orderdate();

# get directory path for $HOME
$HOME = "/s/websitename.com/httpsdocs/data";
my $filename = "/s/websitename.com/httpsdocs/data/cfile.dbf";
open FILE, ">>$filename" or die "Cannot open datafile: $!\n";
flock FILE,LOCK_EX;

my $dbh = DBI->connect("dbi:XBase:$HOME") or die $DBI::errstr;
my $sel = "select * from cfile where acct = ?";
my $sth = $dbh->prepare($sel) or die $dbh->errstr();
# execute the SQL statement with previously declared variables
$sth->execute($acct) or die $sth->errstr();
close FILE;

my @row = $sth->fetchrow_array;
###############################################
# place query results into array as follows
# $row[0] = acct $row[9] = phone
# $row[1] = fname $row[10] = email
# $row[2] = lname $row[11] = gender
# $row[3] = address $row[12] = age (age range)
# $row[4] = city $row[13] = orig_date (date record originated)
# $row[5] = st $row[14] = first_time (if 'n', user hasn't been here before)
# $row[6] = zip $row[15] = edit_date (date record last updated)
# $row[7] = apo $row[16] = loyalbuck
# $row[8] = areacode
my $org_date = substr($row[12],1,10);

# define variables for html page
my $css = "my $js1 = "my $img1 = " #120x75
my $img2 = " #78x50
my $img3 = "my $img4 = " #147x65
my $img5 = " #400x100
my $img6 = " #250x40
my $action1 = "my $action2 = "my $action3 = "my $action4 = "my $action5 = "my $action6 = "my $action7 = "my $action8 = "
my $lb; # loyalbuck value
if ($row[16] eq ""){
$lb = 0;}
else{
$lb = $row[16];}

# HTML page
print "Content-type: text/html\n\n";

print <<EOF;

<HTML>
<HEAD>
<META http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;>

<TITLE>Stay Tuned Re-Order Form</TITLE>
<SCRIPT SRC=&quot;$js1&quot; TYPE=&quot;text/javascript&quot;></SCRIPT>
<LINK rel=&quot;stylesheet&quot; href=&quot;$css&quot; type=&quot;text/css&quot;>
</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot; LINK=&quot;#1E7600&quot; MARGINWIDTH=&quot;0&quot; onLoad=&quot;couponPop()&quot;>

<TABLE WIDTH=&quot;760&quot; BORDER=&quot;0&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot; ALIGN=&quot;center&quot;>
<TR><TD VALIGN=&quot;top&quot;><IMG SRC=&quot;$img1&quot; WIDTH=&quot;120&quot; HEIGHT=&quot;75&quot; BORDER=&quot;0&quot;></TD><TD ALIGN=&quot;center&quot;><IMG SRC=&quot;$img5&quot; WIDTH=&quot;400&quot; HEIGHT=&quot;100&quot; BORDER=&quot;0&quot;></TD><TD ALIGN=&quot;right&quot; VALIGN=&quot;top&quot;><IMG SRC=&quot;$img3&quot; WIDTH=&quot;42&quot; HEIGHT=&quot;1&quot; BORDER=&quot;0&quot; ALIGN=&quot;left&quot;><IMG SRC=&quot;$img2&quot; WIDTH=&quot;78&quot; HEIGHT=&quot;50&quot; ALT=&quot;This is a safe and secure area!&quot; BORDER=&quot;0&quot;></TD></TR>
</TABLE>
<HR SIZE=&quot;1&quot; WIDTH=&quot;85%&quot; NOSHADE>
<FORM METHOD=&quot;post&quot; ACTION=&quot;$action8 &quot; NAME=&quot;frm&quot; onSubmit=&quot;return orderValid(this);&quot;>
<TABLE WIDTH=&quot;500&quot; BORDER=&quot;0&quot; CELLPADDING=&quot;1&quot; CELLSPACING=&quot;0&quot; ALIGN=&quot;center&quot;>
<TR><TD CLASS=&quot;y&quot;>First Name:</TD><TD CLASS=&quot;z&quot;>$row[1]</TD><TD CLASS=&quot;y&quot;>Account Nbr:</TD><TD CLASS=&quot;z&quot;>$row[0]</TD></TR>
<TR><TD CLASS=&quot;y&quot;>Last Name:</TD><TD CLASS=&quot;z&quot;>$row[2]</TD><TD CLASS=&quot;y&quot;>Customer Since:</TD><TD CLASS=&quot;z&quot;>$org_date</TD></TR>
<TR><TD CLASS=&quot;y&quot;>Address:</TD><TD CLASS=&quot;z&quot;>$row[3]</TD><TD CLASS=&quot;y&quot;>Telephone:</TD><TD CLASS=&quot;z&quot;>$row[8]-$row[9]</TD></TR>
<TR><TD CLASS=&quot;y&quot;>City:</TD><TD CLASS=&quot;z&quot;>$row[4]</TD><TD CLASS=&quot;y&quot;>Email Address:</TD><TD CLASS=&quot;z&quot;>$row[10]</TD></TR>
<TR><TD CLASS=&quot;y&quot;>State:</TD><TD CLASS=&quot;z&quot;>$row[5]&nbsp;&nbsp;&nbsp;&nbsp;<SPAN><FONT CLASS=&quot;z&quot;>Zip Code:</FONT></SPAN>&nbsp;$row[6]</TD><TD CLASS=&quot;y&quot;>Loyal Dollars:</TD><TD CLASS=&quot;z&quot;>$lb</TD></TR>
<TR><TD CLASS=&quot;y&quot;>APO/AFO Address:</TD><TD CLASS=&quot;z&quot;>$row[7]</TD></TR>
<TR><TD>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;acct&quot; VALUE=&quot;$acct&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;fname&quot; VALUE=&quot;$row[1]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;lname&quot; VALUE=&quot;$row[2]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;address&quot; VALUE=&quot;$row[3]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;city&quot; VALUE=&quot;$row[4]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;st&quot; VALUE=&quot;$row[5]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;apo&quot; VALUE=&quot;$row[7]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;zip&quot; VALUE=&quot;$row[6]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;areacode&quot; VALUE=&quot;$row[8]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;phone&quot; VALUE=&quot;$row[9]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;email&quot; VALUE=&quot;$row[10]&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;loyaldollar&quot; VALUE=&quot;$row[16]&quot;>
</TD></TR>
</TABLE>
<HR SIZE=&quot;1&quot; WIDTH=&quot;85%&quot; NOSHADE>
<P>

<TABLE WIDTH=&quot;600&quot; BORDER=&quot;1&quot; CELLPADDING=&quot;2&quot; CELLSPACING=&quot;0&quot; ALIGN=&quot;center&quot;>
<TR><TD CLASS=&quot;y&quot; ALIGN=&quot;center&quot;>Item No.</TD><TD CLASS=&quot;y&quot; ALIGN=&quot;center&quot;>Qty</TD><TD CLASS=&quot;y&quot; ALIGN=&quot;center&quot;>Product Description</TD><TD CLASS=&quot;y&quot; ALIGN=&quot;center&quot;>Price</TD><TD CLASS=&quot;y&quot; ALIGN=&quot;center&quot;>Sub-Total</TD></TR>
<TR><TD CLASS=&quot;z&quot; ALIGN=&quot;center&quot;>701</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;qty&quot; SIZE=&quot;1&quot; MAXLENGTH=&quot;3&quot; CLASS=&quot;d&quot; onBlur=&quot;calcSale();&quot;></TD><TD CLASS=&quot;z&quot;><SPAN><FONT CLASS=&quot;y&quot;>Stay Tuned</FONT></SPAN> - <SPAN><FONT CLASS=&quot;t&quot;>Nature's True Body Deodorant</FONT></SPAN> 30 capsules&nbsp;</TD><TD CLASS=&quot;z&quot; ALIGN=&quot;center&quot;>\$19.95</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;itmttl&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; onFocus=&quot;javascript:document.frm.subttl.focus();&quot;></TD></TR>

<TR><TD COLSPAN=&quot;3&quot; ROWSPAN=&quot;6&quot; VALIGN=&quot;top&quot; CLASS=&quot;c&quot; ALIGN=&quot;right&quot;>
<IMG SRC=&quot;$img3&quot; WIDTH=&quot;1&quot; HEIGHT=&quot;29&quot; BORDER=&quot;0&quot;><BR>Loyal Dollar Rewards are automatically deducted&nbsp;&nbsp;<BR><IMG SRC=&quot;$img3&quot; WIDTH=&quot;1&quot; HEIGHT=&quot;38&quot; BORDER=&quot;0&quot;><BR>State sales tax applies to Nevada Residents only&nbsp;&nbsp;
</TD>

<TD CLASS=&quot;c&quot; ALIGN=&quot;center&quot;>Sub-Total</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;subttl&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; onFocus=&quot;javascript:document.frm.loyalbucks.focus();&quot;></TD></TR>

<TR><TD CLASS=&quot;c&quot; ALIGN=&quot;center&quot; NOWRAP>Loyal Dollars</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;loyalbucks&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; onFocus=&quot;javascript:document.frm.coupon.focus();&quot;></TD></TR>

<TR><TD CLASS=&quot;c&quot; ALIGN=&quot;center&quot; NOWRAP>Coupons</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;coupon&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; onFocus=&quot;javascript:document.frm.nv_tax.focus();&quot;></TD></TR>

<TR><TD CLASS=&quot;c&quot; ALIGN=&quot;center&quot;>7&frac14% Sales Tax</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;nv_tax&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; onFocus=&quot;javascript:document.frm.loyalbucks.focus();&quot;></TD></TR>

<TR><TD CLASS=&quot;c&quot; ALIGN=&quot;center&quot;>S & H</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;sh&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; VALUE=&quot;3.85&quot; onFocus=&quot;javascript:document.frm.grandttl.focus();&quot;></TD></TR>

<TR><TD CLASS=&quot;y&quot; ALIGN=&quot;center&quot;>Total</TD><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;text&quot; NAME=&quot;grandttl&quot; SIZE=&quot;6&quot; READONLY CLASS=&quot;d&quot; onFocus=&quot;javascript:document.frm.cctype[0].focus();&quot;></TD></TR>

</TABLE>

<TABLE WIDTH=&quot;380&quot; BORDER=&quot;0&quot; CELLPADDING=&quot;1&quot; CELLSPACING=&quot;0&quot; ALIGN=&quot;center&quot;>
<TR><TD HEIGHT=&quot;10&quot;></TD></TR>
<TR><TD COLSPAN=&quot;2&quot; ALIGN=&quot;center&quot;><A HREF=&quot;javascript:mySubmit();&quot;><IMG SRC=&quot;$img6&quot; WIDTH=&quot;250&quot; HEIGHT=&quot;40&quot; BORDER=&quot;0&quot;></A></TD></TR>
<TR><TD HEIGHT=&quot;10&quot;></TD></TR>
<TR><TD COLSPAN=&quot;2&quot; CLASS=&quot;a&quot; ALIGN=&quot;center&quot;>Credit Card Information</TD></TR>
<TR><TD HEIGHT=&quot;5&quot;></TD></TR>
<TR><TD CLASS=&quot;y&quot; COLSPAN=&quot;2&quot; NOWRAP><INPUT TYPE=&quot;radio&quot; NAME=&quot;cctype&quot; VALUE=&quot;Visa&quot;>&nbsp Visa&nbsp;&nbsp;<INPUT TYPE=&quot;radio&quot; NAME=&quot;cctype&quot; VALUE=&quot;MasterCard&quot;>&nbsp MasterCard&nbsp;&nbsp;<INPUT TYPE=&quot;radio&quot; NAME=&quot;cctype&quot; VALUE=&quot;Discover&quot;>&nbsp Dscover&nbsp;&nbsp;<INPUT TYPE=&quot;radio&quot; NAME=&quot;cctype&quot; VALUE=&quot;Amer Exp&quot;>&nbsp; American Express</TD></TR>
<TR><TD CLASS=&quot;y&quot; NOWRAP>Card Number:</TD><TD NOWRAP><INPUT TYPE=&quot;text&quot; NAME=&quot;cd_nbr&quot; SIZE=&quot;30&quot; CLASS=&quot;d&quot;></TD></TR>
<TR><TD CLASS=&quot;y&quot; NOWRAP>Exp. Date:</TD><TD CLASS=&quot;z&quot;><B>Month:</B>&nbsp;<SELECT NAME=&quot;exp_mo&quot; CLASS=&quot;c&quot;><OPTION SELECTED VALUE=&quot;&quot;><OPTION VALUE=&quot;01&quot;>01<OPTION VALUE=&quot;02&quot;>02<OPTION VALUE=&quot;03&quot;>03<OPTION VALUE=&quot;04&quot;>04<OPTION VALUE=&quot;05&quot;>05<OPTION VALUE=&quot;06&quot;>06<OPTION VALUE=&quot;07&quot;>07<OPTION VALUE=&quot;08&quot;>08<OPTION VALUE=&quot;09&quot;>09<OPTION VALUE=&quot;10&quot;>10<OPTION VALUE=&quot;11&quot;>11<OPTION VALUE=&quot;12&quot;>12</SELECT>&nbsp;&nbsp;<SPAN><FONT CLASS=&quot;f&quot;>Year:</FONT></SPAN>&nbsp;<SELECT NAME=&quot;exp_yr&quot; CLASS=&quot;c&quot;><OPTION SELECTED VALUE=&quot;&quot;><OPTION VALUE=&quot;2002&quot;>2002<OPTION VALUE=&quot;2003&quot;>2003<OPTION VALUE=&quot;2004&quot;>2004<OPTION VALUE=&quot;2005&quot;>2005<OPTION VALUE=&quot;2006&quot;>2006<OPTION VALUE=&quot;2007&quot;>2007<OPTION VALUE=&quot;2008&quot;>2008<OPTION VALUE=&quot;2009&quot;>2009<OPTION VALUE=&quot;2010&quot;>2010<OPTION VALUE=&quot;2011&quot;>2011<OPTION VALUE=&quot;2012&quot;>2012<OPTION VALUE=&quot;2013&quot;>2013<OPTION VALUE=&quot;2014&quot;>2014<OPTION VALUE=&quot;2015&quot;>2015<OPTION VALUE=&quot;2016&quot;>2016<OPTION VALUE=&quot;2017&quot;>2017<OPTION VALUE=&quot;2018&quot;>2018<OPTION VALUE=&quot;2019&quot;>2019<OPTION VALUE=&quot;2020&quot;>2020</SELECT></TD></TR>
<TR><TD CLASS=&quot;y&quot; NOWRAP>Card ID Nbr:</TD><TD NOWRAP><INPUT TYPE=&quot;test&quot; NAME=&quot;cvn&quot; SIZE=&quot;4&quot; CLASS=&quot;d&quot;>&nbsp;&nbsp;<A HREF=&quot;javascript:popIt();&quot; CLASS=&quot;e&quot;>what is this?</A></TD></TR>
<TR><TD CLASS=&quot;y&quot; NOWRAP>Name on Card:</TD><TD NOWRAP><INPUT TYPE=&quot;text&quot; NAME=&quot;cd_name&quot; SIZE=&quot;30&quot; CLASS=&quot;d&quot;></TD></TR>
<TR><TD HEIGHT=&quot;10&quot;></TD></TR>
</TABLE>
<TABLE WIDTH=&quot;645&quot; BORDER=&quot;0&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot; ALIGN=&quot;center&quot;>
<TR><TD HEIGHT=&quot;25&quot;></TD></TR>
<TR><TD ALIGN=&quot;center&quot;><INPUT TYPE=&quot;submit&quot; VALUE=&quot;Submit My Order&quot; CLAS=&quot;b&quot;><IMG SRC=&quot;$img3&quot; WIDTH=&quot;125&quot; HEIGHT=&quot;1&quot; BORDER=&quot;0&quot;><INPUT TYPE=&quot;reset&quot; VALUE=&quot;Clear Form&quot; CLASS=&quot;b&quot;></FORM></TD></TR>
</TABLE></FORM>
<HR SIZE=&quot;2&quot; WIDTH=&quot;85%&quot; NOSHADE>
<TABLE WIDTH=&quot;645&quot; BORDER=&quot;0&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot; ALIGN=&quot;center&quot;>
<TR><TD ALIGN=&quot;center&quot; CLASS=&quot;c&quot; WIDTH=&quot;10&quot; VALIGN=&quot;top&quot;>**</TD><TD CLASS=&quot;c&quot;>Even though this page is secured by 128 bit encryption, you may feel more comfortable by mailing or faxing your order. Complete this form and <A HREF=&quot;javascript:prtOrderPop();&quot;>click here</A> to print your order and then fax or mail the form to our offices.</TD></TR>
</TABLE>
<IMG SRC=&quot;$img3&quot; WIDTH=&quot;1&quot; HEIGHT=&quot;50&quot; BORDER=&quot;0&quot;><BR>
<TABLE WIDTH=&quot;100%&quot; BORDER=&quot;0&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot;>
<TR><TD ALIGN=&quot;center&quot;><A HREF=&quot;$action2&quot; CLASS=&quot;i&quot;>Giveaway Program</A>&nbsp;|&nbsp;<A HREF=&quot;$action3&quot; CLASS=&quot;i&quot;>Loyal Dollars</A>&nbsp;|&nbsp;<A HREF=&quot;$action4&quot; CLASS=&quot;i&quot;>Referrals</A>&nbsp;|&nbsp;<A HREF=&quot;$action5&quot; CLASS=&quot;i&quot;>Order Status</A>&nbsp;|&nbsp;<A HREF=&quot;$action6&quot; CLASS=&quot;i&quot;>Account Manager</A>&nbsp;|&nbsp;<A HREF=&quot;$action7&quot; CLASS=&quot;i&quot;>Main Menu</A></TD></TR>
<TR><TD><HR SIZE=&quot;1&quot; WIDTH=&quot;100%&quot; ALIGN=&quot;LEFT&quot;></TD></TR>
<TR><TD CLASS=&quot;c&quot;><I>Copyright&copy;1999-2002 TVI Distribution Company <A HREF=&quot;$action1&quot;>Legal Notice</A><BR>Last Updated Mon, Aug 5th, 2002</TD></TR>
</TABLE>
</BODY>
</HTML>
EOF

sub orderdate(){
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my ($mo,$dy,$yr,$hr,$mn,$sc,$x);
$x = $mon + 1;
if ($x < 10) {$mo = &quot;0$x&quot;;}
else{
$mo = $x;}
if ($mday < 10) {$dy = &quot;0$mday&quot;;}
else{
$dy = $mday;}
if ($hour < 10) {$hr = &quot;0$hour&quot;;}
else{
$hr = $hour;}
if ($min < 10) {$mn = &quot;0$min&quot;;}
else{
$mn = $min;}
if ($sec < 10) {$sc = &quot;0$sec&quot;;}
else{
$sc = $sec;}
$yr = $year + 1900;
return (&quot;$mo/$dy/$yr $hr:$mn:$sc&quot;);
} There's always a better way...
 
Wow.... I'm glad I didn't ask for some COMPLICATED examples lol. Thanks, I'll try to pick this apart and see if I can extract the info I need from it.
 
This is a complete script that reads a database via an SQL query. The results of the query are dumped into an array (@row). The elements of the array ($row) are the field values of the database. You can see how the values are used to populate the page with data. You can use these values in the exact same way as if you were creating a static web page.

The hardest part of the whole thing is getting the data. Every database has it's own perl library and every ISP has it's own way of allowing access to the directories where your data resides. If your data resides on your own server than you've got it made!

Good luck There's always a better way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top