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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create a web Form that will store the info into a database?

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
0
0
GB
I'm planning on creating a web based form that will collect customer information. I would like these details to be automatically stored into an Access 97 database. The pages will be stored externally on a server but if it easier I could consider hosting the site locally. I have not worked with Forms or CGI before but from researching this task have heard that my objective can be met using them (correct me if i'm wrong!!) The pages are being created in Dreamweaver 4.0 and initially I would like to set up a test example of the collection form using my stand alone machine. Any help would be appriciated. Thanks!
 
First you need a form in which your customers will type their info like this

<TABLE>
<FORM METHOD=POST ACTION=&quot;cgi-bin/tek.pl&quot; name=&quot;customers&quot;>
<!-- the action must point to the script that will be processing the form -->
<TR>
<TD>name</TD>
<TD><INPUT TYPE=&quot;text&quot; NAME=&quot;name&quot;></TD>
</TR>
<TR>
<TD>addr1</TD>
<TD><INPUT TYPE=&quot;text&quot; NAME=&quot;addr1&quot;></TD>
</TR>
<TR>
<TD>addr2</TD>
<TD><INPUT TYPE=&quot;text&quot; NAME=&quot;addr2&quot;></TD>
</TR>
<TR>
<TD>submit</TD>
<TD><INPUT TYPE=&quot;submit&quot;></TD>
</TR>

</FORM>
</TABLE>

on pressing the submit button a cgi script will be called that could look something like this

#!/usr/bin/perl
#shebang not applicable on win32 systems


#use strict; # good practice
use CGI qw( :standard ); # load cgi library
my $q = new CGI; # create new cgi object

# You are saying you are going to use a M$ access db
# so i have opted for w32odbc to talk to the db

use Win32::ODBC; #Load win32 odbc library
my $db=new Win32::ODBC(&quot;export&quot;)or die Win32::ODBC::DumpError();
# create new odbc object the dsn string
# must point to a valid dsn created on the server
# e.g.&quot;DSN=SOME_DSN;UID=USER_ID;PWD=PASSWORD&quot;

my %all = $q->Vars(); # Takes all info from form and sticks it into an
# Associative Array.called %all using the cgi vars method


$db->Sql(qq|INSERT INTO customers (name,addr1,addr2)
VALUES (\'$all{'name'}\',\'$all{'addr1'}\',\'$all{'addr2'}\'|);
# Inserts the values in %all into the relevant fields in the db
# the name addr1 and addr2 correspond to the form element names
# from the form that called this script
$db->Close(); #close the db


#confirmation web page that details have gone into db


print $q->header(),
$q->start_html(-title=>'Simple web form cgi capture',
-bgcolor=>'#003399'),
$q->start_table({-border=>'0',
-bgcolor=>'#cccccc',
-align=>'center'}),
$q->Tr(
$q->th({-colspan=>'2'},'Your details have been stored'),
),
$q->Tr(
$q->td({-align=>'center'},'Name = '),
$q->td({-align=>'center'},$all{'name'}),
),
$q->Tr(
$q->td({-align=>'center'},'addr1 = '),
$q->td({-align=>'center'},$all{'addr1'}),
),
$q->Tr(
$q->td({-align=>'center'},'addr2 = '),
$q->td({-align=>'center'},$all{'addr2'}),
),
$q->end_table,
$q->end_html;


Hope this helps Knowledge is power
Power is Mon£y
 
Thank you. Working to your example first I have been able to create the form. I have also created a database in Access 97 called db2.mdb in a table called Table1 with the field names as you used in your example. I have no knowledge of CGI I'm afraid and I'm not sure how to store the code you provided. I tried just saving it as tek.pl through notepad but all that happens on clicking the submit button is that the code is displayed. Obviously I have made some mistake here!! Also there is a section in the code where you refer to DSN which i've not heard of before. Finally should I change any part of the code to point to the correct Database and table because I can't see any obvious places where the information should go?
 
Ok first of all if you are hosting this site on an ISP you should have a cgi-bin (virtual directory I suggest you read up on these) already created and perl will surely be installed (if they are any kind of ISP). I am sure that they will be very happy to give you any details. So now we can see the ACTION=&quot;cgi-bin/tek.pl&quot; in the form start tag is pointing to a perl script in a directory called cgi-bin directory called tek.pl. This will be run on the server when you press the form submit button and. If you are going to host this site yourself you will have to install perl and and point your web server software to perl. Sounds like you are M$ user so download the win32 easy install binary from and it should automatically set up any file associations on IIS web server for you(if you use the .plx extension it will run with the perlis.dll isapi extension.) Then all you need to do is create a new virtual directory on IIS and point it to the directory you are keeping the perl/cgi scripts in. part 2 to come soon&#61514; Knowledge is power
Power is Mon£y
 
would this work on a linux server( was told i could not use an access database if site was hosted on linux server?
 
Access is not available for Linux, and probably won't be anytime soon. But, you can...

use DBD::mysql;


You can use a MySQL DB instead, which is commonly used with Linux. If you're not familiar with the SQL language, you can use PHPMyAdmin to create your tables rather than through SQL queries. It's quite easy. Just ask your host if they have PHPMyAdmin, or something similar. (By the way, phpwebhosting.com is a great, cheap host.)

It sounds like you're not accustomed to writing code. If you aren't comfortable with HTML, SQL, and have a general knowledge of how to write a program, I think you will ultimately run into problems. Although you may be succesful at installing other people's scripts, and getting the whole thing to run, I can't imagine how you will troubleshoot the thing when something goes wrong, or improve/add features, or more importantly, make sure the code is secure from hackers and whatnot.

I'm not sure what your current knowledge level is, but it would be a really good idea for you to get some good books on any subject you don't already know, such as...

Sams &quot;Teach Yourself SQL in 10 Minutes&quot;

&quot;CGI/Perl&quot; by Diane Zak

...and their are lots of good HTML and relational database tutorials on the you need them.

Good luck,

Greg _______________________________________
constructed from 100% recycled electrons
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top