ASP Tutorial
Name: ASP Tutorial
Author: David Brebner Date: February 10, 1998 Description: Many thanks to David Brebner for providing this excellent beginner's ASP tutorial. Be sure to check out his award winning site Unlimited Realities for other great tutorials. Level: All Levels. By David Brebner What is ASP all about? Well first of all, ASP stands for Active Server Pages. ASP is a server side technology, this means it works on any web browser, because all the work is done at the web server end. Essentially ASP pages are just normal HTML with scripts embedded in them. You can write your scripts in VBScript, Javascript or any language which is ActiveScript compatible. You indicate the start of a script with <%, and the end of a script with a %>. For example; <% for a = 1 to 5 %> <font size= <% = a %> > Hello World </font> <br> <% next %>Will be executed by the server before the page is sent to the browser. The code in the < % .. % > brackets is executed resulting in this html code; <font size= 1 > Hello World </font> <br> <font size= 2 > Hello World </font> <br> <font size= 3 > Hello World </font> <br> <font size= 4 > Hello World </font> <br> <font size= 5 > Hello World </font> <br>Of course the real advantage of ASP is the ability to use ODBC databases with your web pages. Basic ASP objects Before we get into opening databases, I would like to take a moment to discuss the basic structure of the ASP model. Using VBScript, we have access to all the standard VB script objects, any ActiveX DLL's we make in VB5 - and the standard ASP objects. The two most important standard ASP objects are the Request and Response objects. The Request objects allows you to interrogate the URL command line, request.querystring("key"). To request information from a form, request.form("inputname"). Also to retrieve cookies (data stored on the clients machine), request.cookies("key")("data"). The Response object allows you to write to the HTML file. A shorthand version of Response.write "string" is to just simply use an =, as the for next example above does. Also you can write a cookie, response.cookies("adv")("data")="string". Opening a Database First you need to create a database in Microsoft Access, or another database that provides an ODBC driver. Then you should go into the ODBC32 icon in your control panel, and add another system file. The name you give your database is used to find it when you open an ODBC source as below..
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "your database name"
Now you have established a connection to the database we can start to use SQL to retrieve the data.
· TIP: Use Access to build SQL for you. For those of you using Microsoft Access, build a query like normal. To see the SQL behind this pull down the icon on the top left of the toolbar, which normally selects between edit or design mode, and choose to view the SQL statement. You should be able to copy and paste this directly into your ASP code. The next statement builds a quick read only recordset for you to navigate;
set RS = Conn.Execute("SELECT * FROM [table] WHERE [field]=1")
You can then step through the record in the recordset, say writing them to your web page like this;
If not RS.eof then
RS.movefirst
Do
Response.write "Here is some Data " & RS("Field")
Rs.movenext
Loop until RS.eof
End if
When you have to add or edit records, you have to create an explicit object from the ADO object like so;
Const adLockOptimistic = 3
Const adOpenKeyset = 1
Set RS = server.createobject("ADODB.Recordset")
set RS.ActiveConnection = Conn
RS.Open "SELECT * FROM [table]" , , adOpenKeyset, adLockOptimistic
RS.addnew
RS("field")="NewStuff"
RS.update
VB Quiz example
The theory is all well and good, but how can we test it? Well all you need to test this is a web server (Win95 users can use the free Microsoft Personal Web server), ASP dll's (comes with Microsoft web servers) and Microsoft Access 97. And now a real example, this example was also developed as a template for the http://www.VBExplorer web site. Bert has lots of examples to get you started with VB. Basic Design Always a good place to start, what do we want? My idea is to present questions with a range of option buttons for multi-choice answers. When the users submits the form, I would like to tick and cross the responses - and provide a unique response and mark for each answer. At the bottom of the result form a numeric total of the score. Database Quiz Table QuizID, Autonumber QuizName, String QuizHeader, Memo TotalMark, Number Question Table QuestionID, Autonumber QuizID, Number (link to quiz) Number, Number (question number) Question, Memo Options Table OptionID, Autonumber QuestionID, Number (link to question) Option, String Response, String Mark, Number Tick, Yes/NoThat's the basic design of the database. Build those tables in Access, then add the database into the ODBC32 applet as ASPQUIZ. p.s. I usually whip up a quick form in Access to make the data entry easy. ASP coding Lets get the data displaying first, then format it using HTML commands to tidy it up.
<HEAD>
<TITLE>
VBQuiz Example
</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "VBQUIZ"
'now find the quiz specified by the URL command line
'e.g. vbquiz.asp?quiz=1 means show quiz 1
set RS = Conn.Execute("SELECT * FROM [quiz] WHERE [QuizID]=" & request.querystring("Quiz"))
%>
<font face="Arial">
<b><% =rs("QuizName") %></b>
<hr>
<font size=2>
<% =rs("quizheader") %>
<p>
<%
rs.close
'now start the main loop to find all the questions
set RS = Conn.Execute("SELECT * FROM [question] WHERE [QuizID]=" & request.querystring("Quiz") & " ORDER BY [Number]")
if not RS.EOF then
RS.movefirst
do
response.write "<b>" & rs("Number") & ". " & rs("Question") & "</b><p>"
'now display the available options
set ORS = Conn.Execute("SELECT * FROM [option] WHERE [QuestionID]=" & RS("QuestionID") )
if not ORS.EOF then
ORS.movefirst
do
response.write ORS("Option") & "<br>"
ORS.movenext
loop until ORS.EOF
end if
response.write "<p>"
RS.movenext
loop until RS.EOF
end if
conn.close
%>
</BODY></HTML>
You should get a result looking something like this.
The next step is to create a form, so that we can provide option boxes for the user to fill in, and the ability to submit the form to another ASP page called vbmark.asp. A quick overview of forms in HTML, <form action="file to open" method="post"> </form>. Within the body of a form you can use a whole bunch of inputs, like a standard textbox <input type=text name=name>, or a hidden field <input type=hidden name=hiddenname value=something>. We will use <input type=Radio name=Question1 value=Answer1> the radio button to present the possible answers. Also <input type=submit value="submit"> comes in handy as a button to submit the form.
<form action="vbmark.asp?quiz=<% =request.querystring("quiz") %>" method="post">
<%
rs.close
'now start the main loop to find all the questions
set RS = Conn.Execute("SELECT * FROM [question] WHERE [QuizID]=" & request.querystring("Quiz") & " ORDER BY [Number]")
if not RS.EOF then
RS.movefirst
do
response.write "<b>" & rs("Number") & ". " & rs("Question") & "</b><p>" & chr(13)
'now display the available options
set ORS = Conn.Execute("SELECT * FROM [option] WHERE [QuestionID]=" & RS("QuestionID") )
if not ORS.EOF then
ORS.movefirst
do
response.write "<input type=Radio Name=""Question" & RS("QuestionID") & """ Value=""Answer" & ORS("OptionID") & """>" & ORS("Option") & "<br>" & chr(13)
ORS.movenext
loop until ORS.EOF
end if
response.write "<p>"
RS.movenext
loop until RS.EOF
end if
conn.close
%>
<hr>
<input type=submit value="Submit"> Press here to check your answers
</form>
Now it's time to mark the results. VBMark.asp, lets see the resulting code;
<HEAD>
<TITLE>
VBMark Example
</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<%
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "VBQUIZ"
'now find the quiz specified by the URL command line
'e.g. vbmark.asp?quiz=1 means mark quiz 1
set RS = Conn.Execute("SELECT * FROM [quiz] WHERE [QuizID]=" & request.querystring("Quiz"))
totalmark = RS("TotalMark")
%>
<font face="Arial">
<b><% =rs("QuizName") %> - Results</b>
<hr>
<font size=2>
<p>
<%
rs.close
'now start the main loop to mark all the questions
set RS = Conn.Execute("SELECT * FROM [question] WHERE [QuizID]=" & request.querystring("Quiz") & " ORDER BY [Number]")
if not RS.EOF then
RS.movefirst
do
response.write "<b>" & rs("Number") & ". " & rs("Question") & "</b><br>" & chr(13)
'now check the users answer
answer = request.form("Question" & rs("QuestionID"))
'now get rid of the answer, we just want the number on the end
answernum = right(answer,len(answer)-6)
set ORS = Conn.Execute("SELECT * FROM [option] WHERE [OptionID]=" & answernum )
if not ORS.EOF then
ORS.movefirst
response.write " Your Answer : " & ORS("Option") & "<br>"
if ORS("tick") then 'show a tick
response.write "<img SRC="/VBExplorer/"ASPtick.gif"">"
else 'show a cross
response.write "<img SRC="/VBExplorer/"ASPcross.gif"">"
end if
response.write " " & ORS("mark") & " : " & ORS("Response") & chr(13)
summark=summark + ORS("mark")
end if
response.write "<p>"
RS.movenext
loop until RS.EOF
end if
response.write "<hr>You have scored " & summark & "/" & totalmark & "."
conn.close
%>
</BODY></HTML>
A screen shot of this is shown below
The final step is to perhaps add a background texture, and any additional HTML formatting you want. Check it out the working 5 question quiz on my site.
|
Quick searches: Site Search | Advanced Site Search |
|
By using this site you agree to its terms and conditions VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc. |