ASP Basic Syntax Rules
Write Output to a Browser
An ASP file normally contains HTML tags, just like an HTML file. However, an ASP file
can also contain server scripts, surrounded y t!e delimiters "# and #$.
Server scripts are e%ecuted on t!e server, and can contain any e%pressions, statements,
procedures, or operators valid for t!e scripting language you prefer to use.
The response.write Command
T!e response.write command is used to write output to a rowser. T!e following e%ample
sends t!e te%t &Hello 'orld& to t!e rowser(
"!tml$
"ody$
"#
response.write)&Hello 'orld*&+
#$
",ody$
",!tml$
T!ere is also a s!ort!and met!od for t!e response.write command. T!e following
e%ample also sends t!e te%t &Hello 'orld& to t!e rowser(
"!tml$
"ody$
"#
-&Hello 'orld*&
#$
",ody$
",!tml$
Using BScript in ASP
.ou can use several scripting languages in ASP. However, t!e default scripting language
is /0Script(
"!tml$
"ody$
"#
response.write)&Hello 'orld*&+
#$
",ody$
",!tml$
T!e e%ample aove writes &Hello 'orld*& into t!e ody of t!e document.
Using !a"aScript in ASP
To set 1avaScript as t!e default scripting language for a particular page you must insert a
language specification at t!e top of t!e page(
"#2 language-&javascript&#$
"!tml$
"ody$
"#
3esponse.'rite)&Hello 'orld*&+
#$
",ody$
",!tml$
#ote$ 1avaScript is case sensitive* .ou will !ave to write your ASP code wit! uppercase
letters and lowercase letters w!en t!e language re4uires it.
Other Scripting %anguages
ASP is s!ipped wit! /0Script and 1Script )Microsoft5s implementation of 1avaScript+. 6f
you want to script in anot!er language, like P73L, 3788, or Pyt!on, you will !ave to
install script engines for t!em.
/ariale
<html>
<body>
<%
dim name
name="Donald Duck"
response.write("My name is: " & name
%>
<!body>
<!html>
Array
<html>
<body>
<%
Dim "amname(#$i
"amname(% = "&an '(il"
"amname() = "*o+e"
"amname(, = "-e(e"
"amname(. = "/tale"
"amname(0 = "1ai &im"
"amname(# = "2or(e"
3or i = % to #
response.write("amname(i & "<br !>"
4e5t
%>
<!body>
<!html>
<html>
<body>
<%
dim i
"or i=) to 6
response.write("<h" & i & ">-eadin( " & i & "<!h" & i & ">"
ne5t
%>
<!body>
<!html>
<%7 lan(ua(e="8a+ascript" %>
<html>
<body>
<%
+ar d=new Date(
+ar h=d.(et-ours(
9esponse.:rite("<p>"
9esponse.:rite(d
9esponse.:rite("<!p>"
i" (h<),
;
9esponse.:rite("<ood Mornin(="
>
else
;
9esponse.:rite("<ood day="
>
%>
<!body>
<!html>
ASP Procedures
6nsert t!e "#2 language-&language& #$ line aove t!e "!tml$ tag to write t!e
procedure,function in anot!er scripting language(
&xample
"#2 language-&javascript& #$
"!tml$
"!ead$
"#
function jsproc)num9,num:+
;
3esponse.'rite)num9<num:+
=
#$
",!ead$
"ody$
"p$3esult( "#jsproc)>,?+#$",p$
",ody$
",!tml$
'i((erences Between BScript and !a"aScript
'!en calling a /0Script or a 1avaScript procedure from an ASP file written in /0Script,
you can use t!e &call& keyword followed y t!e procedure name. 6f a procedure re4uires
parameters, t!e parameter list must e enclosed in parent!eses w!en using t!e &call&
keyword. 6f you omit t!e &call& keyword, t!e parameter list must not e enclosed in
parent!eses. 6f t!e procedure !as no parameters, t!e parent!eses are optional.
'!en calling a 1avaScript or a /0Script procedure from an ASP file written in
1avaScript, always use parent!eses after t!e procedure name.
ASP )orms and User *nput
T!e 3e4uest.@ueryString and 3e4uest.Aorm commands are used to retrieve user input
from forms.
A form wit! met!od-&get&
How to interact wit! t!e user, wit! t!e 3e4uest.@ueryString command.
A form wit! met!od-&post&
How to interact wit! t!e user, wit! t!e 3e4uest.Aorm command.
A form wit! radio uttons
How to interact wit! t!e user, t!roug! radio uttons, wit! t!e 3e4uest.Aorm command.
User *nput
T!e 3e4uest oject can e used to retrieve user information from forms.
&xample +T,% (orm
"form met!od-&get& action-&simpleform.asp&$
Airst Bame( "input type-&te%t& name-&fname& ,$"r ,$
Last Bame( "input type-&te%t& name-&lname& ,$"r ,$"r ,$
"input type-&sumit& value-&Sumit& ,$
",form$
Cser input can e retrieved wit! t!e 3e4uest.@ueryString or 3e4uest.Aorm command.
Re-uest..ueryString
T!e 3e4uest.@ueryString command is used to collect values in a form wit!
met!od-&get&.
6nformation sent from a form wit! t!e D7T met!od is visile to everyone )it will e
displayed in t!e rowser5s address ar+ and !as limits on t!e amount of information to
send.
6f a user typed &0ill& and &Dates& in t!e HTML form aove, t!e C3L sent to t!e server
would look like t!is(
!ttp(,,www.w>sc!ools.com,simpleform.aspEfname-0illFlname-Dates
Assume t!at &simpleform.asp& contains t!e following ASP script(
"ody$
'elcome
"#
response.write)re4uest.4uerystring)&fname&++
response.write)& & F re4uest.4uerystring)&lname&++
#$
",ody$
T!e rowser will display t!e following in t!e ody of t!e document(
'elcome 0ill Dates
Re-uest.)orm
T!e 3e4uest.Aorm command is used to collect values in a form wit! met!od-&post&.
6nformation sent from a form wit! t!e PGST met!od is invisile to ot!ers and !as no
limits on t!e amount of information to send.
6f a user typed &0ill& and &Dates& in t!e HTML form aove, t!e C3L sent to t!e server
would look like t!is(
!ttp(,,www.w>sc!ools.com,simpleform.asp
Assume t!at &simpleform.asp& contains t!e following ASP script(
"ody$
'elcome
"#
response.write)re4uest.form)&fname&++
response.write)& & F re4uest.form)&lname&++
#$
",ody$
T!e rowser will display t!e following in t!e ody of t!e document(
'elcome 0ill Dates
7%ample for ASP
Test.!tml
<html>
<body>
<"orm action="http:!!localhost!My:eb!test).asp" method="(et">
?our name: <input type="te5t" name=""name" si@e=",%" !>
<input type="submit" +alue="/ubmit" !>
<!"orm>
<!body>
<!html>
test).asp
<html>
<body>
<%
dim "name
"name=9eAuest.Buery/trin((""name"
C" "name<>"" *hen
9esponse.:rite("-ello " & "name & "=<br !>"
9esponse.:rite("-ow are you todayD"
'nd C"
%>
<!body>
<!html>
7%ample :
Test:.!tml
<html>
<body>
<"orm action=" http:!!localhost!My:eb!test,.asp " method="post">
?our name: <input type="te5t" name=""name" si@e=",%" !>
<input type="submit" +alue="/ubmit" !>
<!"orm>
<!body>
<!html>
Test:.asp
<html>
<body>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " fname "!<"r #>")
Response.Write("Ho$ are %ou toda%&")
'nd If
%>
<!body>
<!html>
)orm alidation
Cser input s!ould e validated on t!e rowser w!enever possile )y client scripts+.
0rowser validation is faster and reduces t!e server load.
.ou s!ould consider server validation if t!e user input will e inserted into a dataase. A
good way to validate a form on t!e server is to post t!e form to itself, instead of jumping
to a different page. T!e user will t!en get t!e error messages on t!e same page as t!e
form. T!is makes it easier to discover t!e error.
A Aorm wit! 6nformation
<html>
<body>
<"orm action="demoE"orm,.asp" method="post">
3irst name:
<input type="te5t" name="name" +alue="Donald" !>
<br !>
Fast name:
<input type="te5t" name="name" +alue="Duck" !>
<br !>
<input type="submit" +alue="/ubmit" !>
<!"orm>
<hr !>
<p>*he in"ormation recei+ed "rom the "orm abo+e was:<!p>
<%
C" 9eAuest.3orm("name"<>"" *hen
9esponse.:rite("<p>"
9esponse.:rite("name=" & 9eAuest.3orm("name"
9esponse.:rite("<!p><p>"
9esponse.:rite("*he name propertyGs count is: "
9esponse.:rite(9eAuest.3orm("name".Hount
9esponse.:rite("<!p><p>"
9esponse.:rite("3irst name=" & 9eAuest.3orm("name"()
9esponse.:rite("<!p><p>"
9esponse.:rite("Fast name=" & 9eAuest.3orm("name"(,
9esponse.:rite("<!p>"
'nd i"
%>
<!body>
<!html>
A Aorm wit! 3adiouttons
<html>
<%
dim cars
cars=9eAuest.3orm("cars"
%>
<body>
<"orm action="demoEradiob.asp" method="post">
<p>Ilease select your "a+orite car:<!p>
<input type="radio" name="cars"
<%i" cars="Jol+o" then 9esponse.:rite("checked"%>
+alue="Jol+o">Jol+o<!input>
<br !>
<input type="radio" name="cars"
<%i" cars="/aab" then 9esponse.:rite("checked"%>
+alue="/aab">/aab<!input>
<br !>
<input type="radio" name="cars"
<%i" cars="2M:" then 9esponse.:rite("checked"%>
+alue="2M:">2M:<!input>
<br !><br !>
<input type="submit" +alue="/ubmit" !>
<!"orm>
<%
i" cars<>"" then
9esponse.:rite("<p>?our "a+orite car is: " & cars & "<!p>"
end i"
%>
<!body>
<!html>
A Aorm wit! H!ecko%
<html>
<body>
<%
"ruits=9eAuest.3orm(""ruits"
%>
<"orm action="demoEcheckbo5es.asp" method="post">
<p>:hich o" these "ruits do you pre"er:<!p>
<input type="checkbo5" name=""ruits" +alue="Kpples"
<%i" instr("ruits$"Kpple" then 9esponse.:rite("checked"%>>
Kpple
<br !>
<input type="checkbo5" name=""ruits" +alue="Lran(es"
<%i" instr("ruits$"Lran(es" then 9esponse.:rite("checked"%>>
Lran(e
<br !>
<input type="checkbo5" name=""ruits" +alue="2ananas"
<%i" instr("ruits$"2anana" then 9esponse.:rite("checked"%>>
2anana
<br !>
<input type="submit" +alue="/ubmit">
<!"orm>
<%
i" "ruits<>"" then%>
<p>?ou like: <%9esponse.:rite("ruits%><!p>
<%end i"
%>
<!body>
<!html>