ASP Basics
Basic concepts of programming in ASP
Finally, let us learn ASP concepts we will use over and over again in the next couple of chapters. Let us say that we specify an ASP file named 'dosomething.asp' in the 'ACTION' attribute of a form. Let us say that the form contains a single text input field with the name attribute 'sometext' and a checkbox group with the name attribute set to 'somegroup'.
We can see that the user has entered 'mytext' in the text field and selected the A, B, and D checkboxes. Suppose the form has the 'METHOD' attribute set to POST. When the form is submitted, the data goes to 'dosomething.asp', the script specified in the ACTION attribute. In dosomething.asp, the following ASP objects may be used to access the values the user entered in the form
request.form("sometext") - This contains 'mytext'
request.form("somegroup") - This contains a comma-separated list of
checked members ('A, B, D')
If the form contained 'GET' in the METHOD attribute, dosomething.asp may use the following ASP code to access the values the user entered in the form.
request.querystring("sometext") - This contains 'mytext'
request.querystring("somegroup") - This contains a comma-separated
list of checked members ('A, B, D')
<%response.write "Thanks for your Input"
response.write request.form("sometext")
response.write request.form("somegroup")%>
The above code in dosomething.asp would produce the following output on the screen:
Note the leading less than sign followed by the percent symbol (<%) and the trailing percent symbol followed by the greater than sign(%>). All ASP code is to be enclosed in these tags; otherwise, the server will assume that the text is just HTML and not process commands. If we wanted to format the output in screenshot 7m, we could put it in a table as follows.
====================================================PAGE========Input
<table border=1><tr><td>Thanks for your Input</td></tr>
<tr><td><% = request.form("sometext")%></td></tr>
<tr><td><% = response.write request.form("somegroup")%></td></tr>
====================================================PAGE========Output
The <% = someStringVariable %> is a short hand notation for response.write; it outputs the string value of the specified variable onto the browser. Since 'Thanks for your input' is not an asp directive, we have simply specified it as part of the HTML document. This is a tiny example of a server side script that encapsulates scripting directives in HTML markup.
set conn=Server.CreateObject("ADODB.Connection")
conn.Mode = 3
conn.open "florist","",""
set rs = Server.CreateObject("ADODB.recordset")
The above code creates a database connection object called 'conn', sets its mode to 3 (this setting allows data modification), and opens our florist data source using this connection. Finally, it creates a record set object called rs. This object will hold all the rows that are returned from the database when we send a select command to the database. A select command may be sent to the database in the following manner using the rs recordset object and the conn connection object.
rs.Open "SELECT * From users where login like 'mylogin'", conn
The above command takes a string containing the database command as the first argument and the name of the connection to use as the second argument. Once the database processes this command, it returns the row that contains 'mylogin in the login field. If no such row exists, the rs.eof (end of file) property is true in the returned dataset. We can extract column data from the returned record set in the following way:
if not rs.eof then
response.write(rs("lname"))
response.write(rs("fname"))
response.write(rs("phone"))
end if
Basically, each field's value is stored in rs(fieldname). To move to the next record in the record set if multiple rows are returned, we may use the 'rs.movenext' directive. After data operations, all database connections and recordsets must be closed in the following way
rs.close
set rs = nothing
conn.close
set conn = nothing
We have now learnt how to access form data and print to the browser and thus interact with the user at the client side. We have also learnt how ASP may be used to connect to a database and access information on the database layer. ASP also allows users to set session variables. The server or web host keeps track of each session's variables by assigning a unique session ID to each current HTTP session and referencing a particular session's variables by the session's ID. Session variables therefore pertain to each user's session - they are unique for each client computer connected to the server. Session Variables last as long as the user's computer has an open Internet browser. However, a session may be assigned a 'timeout' value. If a server sets an application or script's session to timeout after twenty minutes, the session variables expire once twenty minutes pass without the user sending a request to the server by submitting a form, clicking on a link and so on. A session variable may be declared in the following way:
session("variableName") = variableValue
For example, a script that processes a login-password form submitted by the user may store the user's login in the following way after the user's password has been verified:
session("login") = request.form("loginname")
The session variable called login now holds the login name the user entered in the form. If we need to check if the user is logged on before showing page 'A', we may use the following code at the start of page 'A' to check if he is logged in and if not, send him to the login page.
if session("login") = "" then
response.redirect("loginpage.htm")
else
Code to Display Page HTML
end if
If the login session variable has not been set, the response.redirect command takes the user to the login page.