Sample ASP Code
The example below is part of an application that gathers product information from several vendors in order to do product comparisons. Vendors enter product information into set form fields.
An ASP file will be used to collect some basic information about each vendor and their products. The file will contain three things: HTML markup to govern the display, ASP code to interact with a database, and some JScript used for verifying what the users input. The ASP code used to accomplish this task is shown below.
Note: Visual Basic is the default language for writing ASP code. You can also set up the server so that Perl is the default language used to write ASP. The code structure will be ASP, but its syntax will be Visual Basic or Perl.
Lines 1 - 5: These are the normal HTML headings.
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <META HTTP-EQUIV="Refresh">
3 <HTML>
4 <HEAD>
5 <TITLE>Vendor Data Input Page</TITLE>
Line 6 - 16: This section is in ASP tags. It connects to the database and retrieves
vendor information based on the 'VendorID' which was set as a session variable
by a previous program. Here the phone number is also reformatted to add the parentheses
and the dash. (Storing the parentheses and the dash in a database when they are always
used isn't really a good habit.)
6 <%
7 Set oConn = Server.CreateObject("ADODB.Connection")
8 oConn.Open "Vendor", "test", "testpw"
9 '---get the vendor data
10 SQLVendor = "Select * from srVendorData where iVendorID = " & session("VendorID")
11 Set RSVendor = oConn.Execute(SQLVendor,,1)
12 InquirePhoneExch = Left(RSVendor("cInquirePhoneNo"),3)
13 InquirePhoneNumber = Right(RSVendor("cInquirePhoneNo"),4)
14 PhoneExch = Left(RSVendor("cPhoneNo"),3)
15 PhoneNumber = Right(RSVendor("cPhoneNo"),4)
16 %>
Line 6 - 16: Here are two functions written in JavaScript. The first updates another frame
on the page. It is called in line 38. The second changes a field to read 'Canada' based on
what is chosen in a drop down list. It is called in line 92.
18 <SCRIPT LANGUAGE="JavaScript">
19 function UpdateMenu(){
20 parent.fraMenu.location.href = "menu.asp";
21 return true;
22 }
23 function CheckForCanada(){
24 if (document.frmVendorData.lstStates.selectedIndex == 3){
25 document.frmVendorData.txtCountry.value = "Canada";
26 return;
27 }
28 if (document.frmVendorData.lstStates.selectedIndex == 6){
29 document.frmVendorData.txtCountry.value = "Canada";
30 return;
31 }
32
33 document.frmVendorData.txtCountry.value = "United States";
34 return;
35 }
36 </SCRIPT>
37 </HEAD>
Line 38: Notice that the 'UpdateMenu' function is run when the page is loaded.
38 <BODY background="<%= application("Background") %>" BGCOLOR="White" onLoad="return UpdateMenu();">
Line 41 to 56: Because ASP can make decisions, it is very easy to use one program to do many
things particularly if those things are similar. Here different responses are printed at the top
of the page depending on how the program is called. This allows one script file to do the job of
gathering data as well as displaying the data after it has been saved and letting the user know
the result of the save. These only print on the page if the program is called with
vendor.asp?Status=OK or vendor.asp?Status=Error
41 <!-- ===================================================== -->
42 <!-- =========== RESPONSES TO RETURNS FROM OTHER PAGES == -->
43 <!--===================================================== -->
44 <% if Request.QueryString("Status") = "OK" then %>
45 <H3><FONT COLOR="Blue">
46 The data has been entered successfully!
47 <BR>Thank you</FONT>.
48 </H3>
49 <% End If %>
50
51 <% if Request.QueryString("Status") = "Error" then %>
52 <BR><IMG SRC="images/Br468_12.jpg" WIDTH=468 HEIGHT=12 BORDER=0>
53 <BR>There has been an error entering the data. Please try again.
54 <BR>Thank you!
55 <BR><IMG SRC="images/Br468_12.jpg" WIDTH=468 HEIGHT=12 BORDER=0>
56 <% End If %>
Line 63 - 69: This is the beginning of the form for the vendor data. When the submit button is clicked,
another script file called 'UpdateVendorData.asp' is run. Notice the value attributes. If the database query
in line 10 found a record, that information is put in the proper controls for editing. Notice also that they are
in double quotes. In most cases double quotes in HTML are optional but here if the data returned contains a space
the data placed in the control will be truncated at the first space.
63 <FORM ACTION="UpdateVendorData.asp" METHOD="POST" NAME=frmVendorData>
64 Company Name
65 <BR><INPUT TYPE="TEXT" NAME="txtVendorName" size=40 value="<%=RSVendor("cCompanyName") %>">
68 <BR>Parent Company
69 <BR><INPUT TYPE="TEXT" NAME="txtParent" size=40 value="<%=RSVendor("cParentCompany") %>">
Line 72 - 87: In line 79 the program gets a list of states that is stored in a table called srGeneralLists. As a convenience,
I normally create one non-normalized table (database talk) that contains lists that I can use for drop down controls
like this. It makes it easier to change the list in the data base then to go though all of your code looking for a list
that may have been used several times. It also makes for shorter code as you'll see in the next section.
72 <!-- ======= ADDRESS ============================ -->
73 <BR>Address
74 <BR><INPUT TYPE="TEXT" NAME="txtAddress1" value="<%= RSVendor("cAddressline1")%>">
76 <TABLE>
77 <%
78 '---get the list of states
79 SQLStates = "Select cMinorGroup, cPhrase from srGeneralLists where cMajorGroup ='States' or cMajorGroup = 'Canada' order by cPhrase"
81 Set RSStates = oConn.Execute(SQLStates,,1)
82 Set session("rsStates") = RSStates
83 %>
84 <TR>
85 <TH align=left>City</TH>
86 <TH align=left>State/Province</TH><TH align=left>ZIP</TH>
87 </TR>
Line 88 - 103: This section creates a drop down list of the states. It got the list of the states from
the database (see line 79 above.) It loops through the list of states looking for a match with the
state that came from the vendors data. If it finds a match it marks it 'SELECTED' otherwise it just
adds it to the controls list.
When the user moves on to the next control, the 'CheckForCanada' function is run.
88 <TR><TD>
90 <INPUT TYPE="TEXT" NAME="txtCity" size=20 value="<%= RSVendor("cCity") %>">
91 </TD>
92 <TD><SELECT NAME="lstStates" onBlur="return CheckForCanada();">
93 <option>
94 <% Do While Not RSStates.EOF
95 If RSVendor("cState") = RSStates("cMinorGroup") then %>
96 <OPTION SELECTED> <%= RSStates("cPhrase") %>
97 <% else %>
98 <OPTION><%= RSStates("cPhrase") %>
99 <% End If %>
100 <% RSStates.MoveNext
101 Loop
102 %>
103 </SELECT></TD>
Line 104 - 114: This is similar to the section above. The program is comparing the vendor data with
an empty string; that is, if there is no data, then use 'United States.'
104 <TD>
105 <INPUT TYPE="TEXT" NAME="txtZip" size=10 value="<%= RSVendor("cZip") %>">
106 </TD>
107 </TABLE>
108 <BR><STRONG>Country</STRONG>
109 <BR><INPUT TYPE="TEXT" NAME="txtCountry" size=40 value=
110 <% If RSVendor("cCountry") = "" then %>
111 "United States">
112 <% Else %>
113 "<%= RSVendor("cCountry") %>">
114 <% End If %>
Lines 117 - 122: Adds the submit button and closes the form and the page.
117 <INPUT TYPE="IMAGE" NAME="imgSubmit" src="images/ENTREC.gif">
118 </FORM>
121 </BODY>
122 </HTML>
© 1998-1999 Anchor Productions, Inc. All rights reserved. ASP code © Buz Warmkessel.