Documentos de Académico
Documentos de Profesional
Documentos de Cultura
ASP is acronym for Active Server Page. It’s Server side script language developed
by Microsoft. When ASP file is request by the browser, the browser does not send
the page back to the client browser instead its content is interpreted & processed by
the server. ASP is also script language that you combine with HTML document if
your server supports ASP. The default language is VBScript. However, you can
specify to use JavaScript or perlScript. If you are building E-commerce page, ASP is
your ideal language or one of the languages on your choice list.
Tools
In order to build ASP pages on your PC, first you need to install PWS [Microsoft
Personal Web Server] or IIS [Internet Information Server] for the internet. You can
download PWS from Microsoft web site: here . You can also learn how to set up
here.
Otherwise you need host a that provides ASP server. see list of hosts.
Here is how to write hello world program in ASP:
<html>
<head>
<title>hello world program</title>
</head>
<body>
<% Response.write(“Hello World”) %>
</body>
</html>
This program declares a variable and writes it:
<html>
<body>
<%
dim h
h="Hello World"
response.write("Say: " & h)
%>
</body>
</html>
This program writes the current time:
<html>
<body>
It’s now <%=Time()%>
</body>
</html>
ASP Arrays
<%
dim cars(3)
dim x
cars(0)="Jeep Grand Cherokee"
cars(1)="Jeep Wrangler" Jeep Grand Cherokee
The variable x which
cars(2)="Jeep Liberty" Jeep Wrangler
starts from 0 to 3 acts as
cars(3)="Jeep Cherokee Jeep Liberty
an index for the array.
Briarwood" Jeep Cherokee Briarwood
for x= 0 to 3
response.write(cars(x)&"
")
next%>
Two dimensional Arrays
You can define two or more dimensional arrays by puting numbers withing the
parenthesis. Here is how you would define two dimensional array Dim cars(3,3).
The reason you may use this type of array is when you have records of each item.
For example, car, year, price,... and you want display one record at a time.
The following is an example of two dimensional array:
This is an asp sub procedure that is used to write information stored in variables:
<%
Sub GetInfo()
dim name,telephone,fee This example simply declares, populates,
name="Mr. John Doe" & writes three variables in the asp sub
telephone="555-5555" procedure, 'GetInfo'. This sub is executed
fee=20 right after the end sub.
Response.write("Name: "& name
&"<br>") Here is the execution result:
Response.write("Telephone: "& telephone
&"<br>") Name: Mr. John Doe
Response.write("Fee: "& fee &"<br>") Telephone: 555-5555
End Sub Fee: 20
GetInfo()
%>
You can pass an argument to the asp sub procedure, and provide the value(s) when
calling it.
This is an asp sub procedure that accepts an argument:
<%
function userName()
userName="MaryLou"
end function
%>
This function procedure remembers a username. You can use this function any
number of times in your program, and you can change it once to maintain it- use
'userName()' to call this function. For example: response.write("The username is:
"&userName())
<%
Function total(price)
dim tax
tax=price*.09
total=price+tax
end Function
%>
The price value is provided when calling the function- Example: response.write("The
total price is: " & total(50))
This is an asp function procedure that accepts an argument:
<%
Function profit(sellPrice, cost)
dim pr
Here is the execution result:
profit=sellPrice-cost
End Function
Profit: $390
dim currProfitcurrProfit=profit(1280,890)
Response.write("Profit: $"&currProfit)
%>
ASP If Statements
Using if statement, ASP has the ability to make distinctions between different
possibilities. For example, you might have a script that checks if a variable consists
certain type of values.
Here is syntax for asp if statement.
<%
if condition is true then
do something
else
do something else
end if
%>
Check the following if statement that checks if a variable has a value equal to 1.
<%
dim n
n =1
if n= 1 then
response.write("N has the value Result:
equal to 1") N has the value
else equal to 1
response.write("N is not equal to
one")
end if
%>
Nested If Statement
Case statement can be used instead of if statement suitably when one condition
could have multiple possibilities. Following example illustrates the use of case
statement
<% Dim dat Result:
dat=WeekDay(Date) Today is Thursday
%> This example is to show the use of case
<% statement and there is better way to
Select Case dat show the day of the week in asp. The
case 1 variable dat is set to the day of the week
response.write("Today is Sunday") and it could have values from 1 to 7, 1 is
case 2 Sunday and 7 is Saturday. Case
response.write("Today is Monday") statement is used to display the day of
case 3 the week according to the value of
response.write("Today is Tuesday") variable dat.
case 4
response.write("Today is Wednesday")
case 5
response.write("Today is Thursday")
case 6
response.write("Today is Friday")
case 7
response.write("Today is Saturday")
end select
%>
If Statements Loops
ASP Loop Statements
Loops are set of instructions that repeat elements in specific number of times.
Counter variable is used to increment or decrement with each repetition of the loop.
The two major groups of loops are, For..Next and Do..Loop.While..Wend is another
type of Do..Loop. The For statements are best used when you want to perform a
loop in specific number of times. The Do and While statements are best used to
perform a loop an undetermined number of times.
This is a For..Next example which counts from 1 to 5.
<%
Result:
Dim counter
The counter is: 0
counter=0
The counter is: 1
for counter = 0 to 5
The counter is: 2
response.write("The counter is:
The counter is: 3
"&counter&"<br>")
The counter is: 4
next
The counter is: 5
%>
The above example increments a variable counter from 0 to 5 The values of counter
are incremented by 1 on each run before 6. We can modified how the values are
incremented by adding step # to the for counter statement.
Here is For..Next example incrementing values by two.
<%
Dim counter
counter=0 Result:
for counter = 0 to 5 step 2 The counter is: 0
response.write("The counter is: The counter is: 2
"&counter&"<br>") The counter is: 4
next
%>
The following example increments a variable counter from 5 to 0 The values are
decremented by 1. It's first example with reversed order.
<% Result:
Dim counter The counter is: 5
counter=0 The counter is: 4
for counter = 5 to 0 step -1 The counter is: 3
response.write("The counter is: The counter is: 2
"&counter&"<br>") The counter is: 1
next
The counter is: 0
%>
Do Loop
This Do..Until loop example performs the test at the start of the loop:
<%
Dim counter
Result:
counter=5
The counter is: 4
Do
The counter is: 3
response.write("The counter is:
The counter is: 2
"&counter&"<br>")
The counter is: 1
counter=counter-1
The counter is: 0
loop until counter =0
%>
You can also accomplish the loop this way:
<%
Dim counter
Result:
counter=5
The counter is: 4
Do Until counter=0
The counter is: 3
response.write("The counter is:
The counter is: 2
"&counter&"<br>")
The counter is: 1
counter=counter-1
The counter is: 0
loop
%>
Displaying Date & Time with ASP
You use time() to get the current time, Date() to get the current date, and now() to
get the current date & time.
For example, these examples will Illustrate use of date and time functions.
Example Result
<%=time()%> 11:41:44 PM
<%=date()%> 11/18/2010
<%=now()%> 11/18/2010 11:41:44 PM
<%response.write("Current date & time: "&now()) Current date & time: 11/18/2010
%> 11:41:44 PM
<%= FormatDateTime(Date, 0)%> 11/18/2010
<%= FormatDateTime(Date, 1)%> Thursday, November 18, 2010
<%= FormatDateTime(now, 3)%> 11:41:44 PM
<%= FormatDateTime(now, 4)%> 23:41
<%response.write("Day of the week:
Day of the week: 5
"&WeekDay(Date))%>
<%response.write("Day of the month:
Day of the month: 18
"&Day(Date))%>
<%response.write("Current Year: "&Year(Date))%> Current Year: 2010
<%response.write("Current Year:
Current Year: 10
"&Right(Year(Date),2))%>
<%response.write("Today is:
Today is: Thursday
"&WeekDayName(WeekDay(Date)))%>
<%response.write("Hour Part: "&(hour(now)))%> Hour part: 23
<%response.write("Minute Part: "&Minute(now()))
Minute part: 41
%>
<%response.write("Second Part: "&Second(now()))
Second part: 44
%>
Processing forms using ASP
You can process HTML forms using these two powerful ASP objects, Response and
Request. Response outputs the value to a page and Request retrieves values
from an object. Take a look at the following example:
Result
Top of Form
<form name=”userForm” method=”post”
action=”userForm.asp”> Enter a user name:
Enter a user name: <input type=”text”
name=”userName” size=”20”>
Enter a password: <input type=”password” Enter a password:
name=”password” size=”20”>
<inpute type=”submit” name=”submit” value=”Send”>
</form>
Send
Bottom of Form
We just created HTML form and tell the browser to process the form using the file
"userForm.asp". The following is userForm.asp file that writes the values from the
form.
You can store value retrieved from form into variables in order to use these value
whatever way you want. Take a look these at this example which is little bit more
complex then previous one.
<html>
<head>
<title>Form Example 2</title>
</head>
<body>
<p><b>This example process basic form elements</b>
<form method="POST" action="formProcess.asp">
<p>Your name: <input type="text" name="Name" size="20"><br>
Status: <input type="radio" value="Customer" name="status">Customer <input
type="radio" name="status" value="Visitor">Visitor<br>
Do you own any of these trucks:<br>
<input type="checkbox" name="truck" value="Land Cruiser">Land Cruiser<br>
<input type="checkbox" name="truck" value="Sequoia">Sequoia<br>
<input TYPE="checkbox" name="truck" value="4Runner">4Runner<br>
<input TYPE="checkbox" name="truck" value="Highlander">Highlander<br>
<input TYPE="checkbox" name="truck" value="Tundra Access Cab">Tundra Access
Cab<br>
Car of Choice:<select size="1" name="product">
<option value="MR2 Spyder">MR2 Spyder</option>
<option value="Celica">Celica</option>
<option value="Matrix">Matrix</option>
<option value="Avalon">Avalon</option>
<option value="Camry">Camry</option>
<option value="Corolla">Corolla</option>
<option value="Echo">Echo</option>
<option value="Prius">Prius</option>
<option value="RAV4 EV">RAV4 EV</option>
</select><br>
Enter some general comments about what you think about Toyota cars:<br>
<textarea rows="5" name="Comments" cols="50"></textarea><br>
<align="center"><input type="submit" value="Submit" name="submit"><br>
</form>
</body>
</html>
Result of the above code
This example process basic
form elements
Top of Form
Your name:
Code for formProcess.asp
Status: Customer Visitor <html>
Do you own any of these trucks: <head>
<title>Result of your information</title>
</head>
Land Cruiser
<body>
<%
Sequoia dim name, status, truck, car, comments
name=Request.Form("Name")
4Runner status=Request.Form("status")
car=Request.Form("car")
comments=Request.Form("comments")
Highlander truck=Request.Form("truck")
%>
Tundra Access Cab Your name: <b><%=name%></b><br>
MR2 Spyder
Status: <b><%=status%></b><br>
Your favourite car is: <b><%=car%></b><br>
Car of Choice:
You currently own these trucks:<b><%=truck
Any comments about Toyota cars:
%></b><br>
Your comments about Toyota products:<b><
%=comments%></b>
</body>
Submit
Bottom of Form
<html>
<title>Create txt file </title>
<body>
<%
Set
fileObj=Server.CreateObject("Scripting.Fil Set
eSystemObject") fileObj=Server.CreateObject("Scripting.Fil
set eSystemObject") creates the instance of
file1=fileObj.CreateTextFile("\asp\TextFile. the file object. set
txt") file1=fileObj.CreateTextFile("\asp\TextFile
file1.WriteLine("This is what goes to the .txt") creates the actual file, TextFile.txt
text file that would be created") within the specified path. WriteLine()
file1.WriteLine("This is the second line of writes the quoted line of text then we
the text file") close the file and set instance back to
file1.Close nothing.
set file1=nothing
set fileObj=nothing
%>
</body>
</html>
Reading from a Text File
Reading from a text file is also very easy and similar to writing to it. The following
example illustrates how to read from a text file.
<html> Result
<body> Welcome
<% this is line one
Set this is line two
fileObj=Server.CreateObject("Scripting.FileSystemObject") this is line three
Set this is line four
listFile=fileObj.OpenTextFile(Server.MapPath("\cliktoprogram\ this is line five
asp\list.txt"), 1) This example displays
do while listFile.AtEndOfStream = false all the lines of the text
Response.Write(listFile.ReadLine) file one by one.
Response.Write("<br>")
loop
listFile.Close
Set listFile=Nothing
Set fileObj=Nothing
%>
</body>
</html>
To display all the lines at once without line breaks, simply replace the lines starting
with do while and ending with loop to Response.Write(listFile.ReadAll).
tblFeeds
Autonumb
user_id 8
er
Name Text 45
Commen
Text 200<>
ts
To add a data to the table tblFeeds, first we will have to create a form to enter the
data and then open the database and make the connection.
Here is the form to enter the data before adding to the database:
<head> Name:
<title> Adding to database example </title>
<script type="text/javascript">
<!--
function validate()
{
if(document.form.name.value=="")
{
alert("Name is missing");
return false;
}
if(document.form.comments.value.length<8) Commen
{ ts:
alert("Not enough comments entered");
return false;
}
else
{ Submit
return true;
} Bottom of Form
}
This form takes name and comments
//-->
and passes to save.asp file to be
</script> processed. Blank value will not pass to
</head> the save.asp file since we are preventing
<body> that using javascript. Enter some data
<form name="form" method="post" and hit the submit button.
action="save.asp"> Save the file as EnterData.html or
Name: <input type="text" name="name" EnterData.asp.
maxlength="45"><br>
Comments: <textarea cols="20" rows="8"
name="comments"
maxlength="200"> </textarea><br>
<input type="submit" name="Save"
value="Submit" onClick="return validate();">
</form>
</body>
</html>
Now, you insert the new record to the database using the information provided through
EnterData.asp. Here is the code to do this:
save.asp
<%
Dim Conn
Dim Rs
Dim sql
'Create an ADO connection and recordset object
Set Conn = Server.CreateObject("ADODB.Connection")
Set Rs = Server.CreateObject("ADODB.Recordset")
'Set an active connection and select fields from the database
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="
&Server.MapPath("FeedBack.mdb")
sql= "SELECT name, comments FROM tblFeeds;"
The third field (user_no) of our table is auto generated and sequentially will
accommulateit self on each addition of new record.
You redirect the user to another page when the record is added to the database
using
<%response.redirect("view.asp")%>
After the data is added to the database, the next thing you may want do is view to see what is
added. The code is very similar.
This code bellow displays the fields.
view.asp
<%
Dim Conn
Dim Rs
Dim sql
Set Conn = Server.CreateObject("ADODB.Connection")
Set Rs = Server.CreateObject("ADODB.Recordset")
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="
&Server.MapPath("FeedBack.mdb")
sql= "SELECT name, comments FROM tblFeeds;"
Rs.Opensql, Conn
Do While not Rs.EOF
Response.Write
("============================================="&"<br
>")
Response.Write ("Name: " & "<font color='red'>" &Rs("name") & "</font>")
Response.Write ("<br>")
Response.Write ("Comment: " & "<font color='red'>" &Rs("comments") &
"</font>")
Response.Write ("<br>")
Rs.MoveNext
Loop
Rs.Close
Set Rs = Nothing
Set Conn = Nothing
%>
View this file
Update a record
There are more than one way to do things. For this example, we are going to list
items from the database so that you can select a record using radio button.
After the new data is entered, the next thing is to save the data and the following
is the file to do that.