iwats asp
Start
 
News
 
Course Notes
Intro
What is ASP?
Uses
Requirements
Basic syntax
Response
Request
IF and CASE
Includes
Sessions
Common Errors
Using Your Code
 
Course Test
 
Resources
IF...THEN and CASE

By themselves, response and request don't have much usefulness. Only with some sort of checking or conditional code do they become useful. This is where IF statements and CASE statements come in. The example of two simple pages to generate some navigation. The first page (below) contains a simple form:

<HTML>
<HEAD>
<TITLE>My navigation form</TITLE>
</HEAD>
<BODY>
<H1>My navigation form</H1>
<FORM ACTION="navigate.asp" METHOD="Post">
<SELECT NAME="select">
<OPTION VALUE="1">TIE Corps.org</OPTION>
<OPTION VALUE="2">Dark Brotherhood.org</OPTION>
<OPTION VALUE="3">Emperor's Hammer.org</OPTION>
</SELECT>
<BR>
<INPUT NAME="Submit" TYPE="submit" VALUE="Hit me!">
</FORM>
</BODY>
</HTML>

This form will post the selection the user picks from the dropdown box. The selection will be posted to a file called navigate.asp Below is the navigate.asp file's contents.

<%
' It is always good to comment your code so you can keep track of things you do.
' This code below will use 3 IF statements to go through our submitted form

' The dropdown box is the field we want to check the value of, so in our request.form
' command, the field we're checking is "select"

IF request.form("select") = 1 THEN
response.redirect "http://www.emperorshammer.net"
END IF
' IF...THEN statements are pretty basic. We ask, if the value of the form submitted is
' equal to one, then go to www.emperorshammer.net

IF request.form("select") = 2 THEN
response.redirect "http://www.darkjedibrotherhood.org"
END IF
' If its equal to 2, then go to the DB site

IF request.form("select") = 3 THEN
response.redirect "http://www.emperorshammer.org"
END IF
' And if equal to 3, go to the EH domain
%>

This isn't a very good way of coding such a script, because it involves 3 separate IF�THEN statements. Below is a more "correct" coding

<%
' It is always good to comment your code so you can keep track of things you do.
' This code below will use a recursive IF statement to go through our submitted form

' The dropdown box is the field we want to check the value of, so in our request.form
' command, the field we're checking is "select"

IF request.form("select") = 1 THEN
response.redirect "http://www.emperorshammer.net"
' IF...THEN statements are pretty basic. We ask, if the value of the form submitted is
' equal to one, then go to www.emperorshammer.net

ELSEIF request.form("select") = 2 THEN
response.redirect "http://www.darkjedibrotherhood.org"
' If its equal to 2, then go to the DB site

ELSEIF request.form("select") = 3 THEN
response.redirect "http://www.emperorshammer.org"
END IF
' And if equal to 3, go to the EH domain
%>

In this case, there is one IF�THEN statement and the server will move onto the next condition checking (the next ELSEIF) if the first IF comparison is false. Similar code can be used to handle request.querystring. Below is an example page with several links:

<HTML>
<HEAD>
<TITLE>Request.querystring example</TITLE>
</HEAD>
<BODY>
<H1>Request.querystring example</H1>
<A HREF="greeting.asp?ID=1">Hi</A>
<BR>
<A HREF="greeting.asp?ID=2">Hello</A>
<BR>
<A HREF="greeting.asp?ID=3">Good Day</A>
<BR>
<A HREF="greeting.asp?ID=4">Guten Tag</A>
</BODY>
</HTML>

These links are all pointing towards the page greeting.asp, with a querystring called ID. IF...THEN can be used to handle these. Below is the code for greeting.asp:

<%
' This is code to handle our request.querys from the previous page
' we get the "ID" bit in brackets because it's the text after the greeting.asp? part of the
' link. It can change depending on how you make your links.
IF request.querystring("ID") = 1 THEN
response.write "Hi!"
ELSEIF request.querystring("ID") = 2 THEN
response.write "Hello!"
ELSEIF request.querystring("ID") = 3 THEN
response.write "Good day!"
ELSEIF request.querystring("ID") = 4 THEN
response.write "Guten Tag!"
END IF
' In this case, we have used response.write and some text for each possible condition
%>

IF...THEN can grow quite large, be prone to errors and is slower to execute. The solution to this problem is to use SELECT CASE. It's specifically designed for comparing one variable against multiple conditions. Below is the greeting script, reworked using SELECT CASE:

<%
' This is code to handle our request.querys from the previous page
' This time we are using SELECT CASE

' here we are saying we want to use the querystring to compare against
SELECT CASE request.querystring("ID")

' these are all basically condensed versions of IF...THEN statements
CASE "1"
response.write "Hi!"
CASE "2"
response.write "Hello!"
CASE "3"
response.write "Good day!"
CASE "4"
response.write "Guten Tag!"
' This is an addition. It basically says "If the case submitted isn't covered by the
' previous items, then do this". In this case, we're covering ourselves against
' people manually entering invalid numbers in the URL
CASE ELSE
response.write "You're messing with my program"
END SELECT
%>

You could also use a SELECT CASE for the dropdown box navigation form in the first IF...THEN example. It would just be a simple matter of changing the request.querystring to the appropriate request.form and changing the case comparisons.