SOURCE CODE:
<html><head><title>Gift Finder</title></head>
<body bgcolor="#FFFF99" text="#800000">
<h2>Gift Finder (Select Case)</h2>
<form method="post" name="frm"
action="javascript:void(0)">
<p>Gender
<select size="1" name="txtGender">
<option value="M">Male</option>
<option value="F">Female</option>
</select>
Price Range
<select size="1" name="txtPrice">
<option value="1">Under $25</option>
<option value="2">$25-$50 </option>
<option value="3">$50 and up</option>
</select></p>
<p>
<input type="button" value="Submit"
name="btnSubmit" onClick="getGift()";>
</p>
</form>
<script language="vbscript">
function getGift()
dim strP, strG, strGP
strP = document.frm.txtPrice.value
strG = document.frm.txtGender.value
strGP = strG & StrP & ".asp"
if (strG = "M") then
select case strP
case 1
window.alert("Go to " & strGP & " ?")
case 2
window.alert("Go to " & strGP & " ?")
case else
window.alert("Go to " & strGP & " ?")
end select
else
select case strP
case 1
window.alert("Go to " & strGP & " ?")
case 2
window.alert("Go to " & strGP & " ?")
case else
window.alert("Go to " & strGP & " ?")
end select
end if
end function
</script>
</body></html>
We could replace the window.alert statement with window.location.href=strGP
to go to a different page, depending upon the gender and price range. In this
example, the SELECT CASE statement is not needed at all. We could simply jump to
the strGP page.
You will never have the exact same lines of code under different cases. If you do, you don't need to use the select case statement at all. Just use one line of code that redirects to the page stored in the variable. The select case statement allows us to run different lines of code under different conditions, depending upon the value of condition variable.
The select case statement is used when we want to run different code based on a different condition or user response. For example, if we wanted to assign a pizza price based on the size selected, we could use a case statement for each possible size and set the price based on the size selected. Or, we could send a credit card customer to a different URL to authorize the on-line purchase, depending upon the credit card selected.