ASP as you might think is 'Active Server Pages' or you can say 'Visual Basic Scripting' too and was build by Microsoft from the famous 'Visual Basic' software developer program.
To follow with this tutorial, you must have a server running (PWS - Personal Webserver, Ommi HTTP...), any HTTP server that supports ASP scripting. If you're using Windows 98, you can install it by inserting the Windows 98 CD into you CD-ROM and going to the sub-directory 'Add-ons' and following up into 'PWS' and you click Install.exe (I guess that might be this the file or if not, check Setup.exe).
Make the Default Install from the CD and the installation will prompt you for a place to serve your files, and you keep the default directory (might be 'C:\Inetpub\wwwroot').
After the installation, you might have to reboot your machine and when Windows restart you should see an icon in the Task Bar. Double click it and you should see a window with an option 'Advanced'. Click this option and it will show you the virtual folders and check the box at bottom saying: 'Allow search in this folder' or something like this (it's because mine is in Portuguese) and after this you might see a button named 'Edit properties' and after you click it check the box saying 'Execute'.
And we're now ready to start learning ASP and just one thing, you should put your files (your HTML and ASP files into the directory C:\Inetpub\wwwroot) but not in the root folder, create a folder for your files and you can access then by opening your IE and typing: http://localhost/your_folder_name.
ASP Tutorial - Part IITo use ASP, you must start it with open tags and finish it with closing tags, just like HTML codes. The tag that you use to open the ASP code is: <% and to close you use %> and as you can see it's very simple.
An example of an ASP program:
<% Response.Write("Hello World!") %>
The explanation of this code:
<% opens the ASP code
Response.Write use this to send a response to the browser. The string, number or any kind of thing that you want to pass to the browser must be between the (" and the ") [ There is a difference when using variables or table fields, that we'll see later]
%> closes the ASP code
So, as you see, ASP can be as simple as you thing. I'm going to show you now how to declare and use variables in your ASP script.
To declare a variable, use this code: Dim
Example:
Dim MyVariable
A variable in ASP doesn't have the $ like in PERL, PHP, C... and a variable name must start with a letter or a number and this name can contain underscores ( _ ) and can't have any space in the name and you can use long names in your variables.
To use comments in your ASP scripts, you should use the ' character.
Example:
<% Dim MyVariable 'This is a comment %>
So, everything after the ' is a comment, but this kind of comment is only for one line, so if you write multiple comments in multiple lines, don't forget to insert a ' before every line.
Example:
<%
Dim MyVariable 'This is comment
'And this comment begins in this line
'and finish in this one
but this is not a part of the comment
'and will make your script get an error
%>
So, now you know how to declare a variable, make a comment and send a response to the browser, so let's now give a value to a variable and use it to send a response to the browser.
<%
Dim MyVariable
MyVariable = "This is a simple text."
'Sending this variable value to the browser
Response.Write (MyVariable)
%>
Explaining the code:
<% opens the ASP code
Dim declares a variable. Tip: to declare more than one variable, use this: Dim var1, var2, var3...
MyVariable = attribute a value to a variable. This value must be between the " and " if is a string and don't need to be between the " and " if is a number (numerical). If you declare a number in a variable between the " and " the script will treat it as string, not as numerical. And remember that MyVariable is the name of your variable and could be any name.
' make a comment in your code
Response.Write -> Send a response to the browser and as I said before, this response must be between the (" and "), but is this response is located in a variable, you need to eliminate the " and ", so using only the ( and ).
%> closes the ASP code
ASP Tutorial - Part III
In this part, we're going to learn some basics of arrays and some tricks with variables.
Let's say that you have two variables:
<%
Dim name, surname, fullname
'Giving a value to the variables
name = "Ramiro"
surname = "Junior"
fullname = name & surname
Response.Write (fullname)
%>
Explaining the code:
<% opens the ASP code
Dim declares a variable
' makes a comment
name = attribute a value to a variable. the name of this variable is 'name' and the variable name can be any name, as I said before.
surname = attribute a value to a variable
fullname = attribute a value to a variable, but in this case, we attribute the value of two variables into one and concatenate both. The '&' character concatenate string variables.
Response.Write sends a response to the browser
%> closes the ASP code
So, as you can see, the & concatenate string variables. So let's get into details.
& concatenates string variables
i.e.: var1 = var2 & var3+ concatenates and/or add numerical variables
i.e.: total = price + postal_fee- subtract a value from a numerical variable
i.e.: total = price - postal_fee* multiply numerical variables.
i.e.: total = price * quantity/ divides numerical variables.
i.e.: total = price / quantity% return the rest of the division of a variable.
i.e.: total = price % quantity
By now, you should know how to open an ASP code, declare variables, assign a value to them, make a comment, concatenate variables and make use of math properties with them and send a response to the browser. I'm trying to be as easy as it can be, by teaching ASP divided into several parts and make everything explained. I will appreciate any question, suggestion or comment that you want to do.
Arrays: an array is a collection of values contained into one "big variable". But there is one thing that I need to explain before you use Array. Arrays always begin with the 0 as the first record or index. You'll see it below in the example.
To declare an array, use this syntax:
<%
Dim MyArray(2) 'Array with 3 elements
MyArray(0) = "Ramiro"
MyArray(1) = "Varandas"
MyArray(2) = "Junior"
'Concatenating the array into a variable
MyNameIs = MyArray(0) & MyArray(1) & myArray(2)
Response.Write(MyNameIs)
%>
Explaining the code:
<% open the ASP code
Dim MyArray(2) declares a variable or an array, and uses the name "MyArray" to assign it as the name of the array and use the index "(2)" to set the array to have 3 elements. Remembering that arrays begins with 0 as the 1st record or index.
MyArray(0) assign a value to the 1st index
MyArray(1) assign a value to the 2nd index
MyArray(2) assign a value to the 3rd index
' make a comment
MyNameIs = Create a variable, that i haven't declared that will concatenate the 3 values of the array into one.
Response.Write Send a response to the browser
%> closes the ASP code
As you can see arrays aren't that thing, so it's not so difficult to use them, but there's a special trick with arrays, that is:
ReDim MyArray(3)
This command will make a resize of the size of 3 values or index into 4 values or index. So, by doing this you can make this code:
<%
Dim MyArray(2)
MyArray(0) = "Ramiro"
MyArray(1) = "Varandas"
MyArray(2) = "Junior"
ReDim MyArray(3)
MyArray(1) = "Fernando"
'Concatenate the array into a variable
MyNameIs = MyArray(0) & MyArray(1) & MyArray(2) & MyArray(3)
Response.Write(MyNameIs)
%>
So, to resize an array use:
ReDim Name_Of_The_Array(number)
Remember that the number must be bigger than the old one, if when you created the array the value was 3, now in the resize you need to put 4 else if you'll loose data.
ASP Tutorial - Part IV
If Is a conditional test and it always accept a value, that must be passed, so that the script can validate it as true or false. If true, the instruction below the If will be executed, if false, you have three options for the false: Else If, Else and finishing the If test.
ElseIf Is the same as If, and accepts a value to be passed for the validation. But remind this: Else If accepts a value but Else doesn't.
Else Is normally used when you make a If test that needs to check if is one value or other. Let's see all of these things in an example:
<%
Dim MyName, MySurname
MyName = "Ramiro"
MySurname = "Junior"
If MyName = MySurname Then
Response.Write("My first name and my surname are equal - This is false")
Else
Response.Write("My first name and my surname are different - This is true")
End If
%>
Explaining the code:
<% opens the ASP code
Dim declare a variable or an array
If begins the If statement
MyName = MySurname this is the value to be checked, in this case, the If will see if MyName (Ramiro) is equal to MySurname (Junior). The If should validate it as false. In ASP, this verification isn't case sensitive, so this makes Ramiro, ramiro, RAMIRO all the same and Junior, junior, JUNIOR all the same too, but there's a internal function that can validate the string by case and we'll see it later.
Then Will make the code below it to be executed, if true.
Response.Write Send a response to the browser
Else It will be executed only if the IF is false. Remember that Else don't accept a value to be checked.
Response.Write sends a response to browser
End If ends the If statement
%> close the ASP code
Now let's take the same example, but we're going to use Else If:
<%
Dim MyName, MySurname
MyName = "Ramiro"
MyMiddleName = "Varandas"
MySurname = "Junior"
If MyName = MySurname Then
Response.Write("This is false")
ElseIf MyName <> MyMiddleName Then
Response.Write("This is true, because Ramiro is different from Varandas")
Else
Response.Write("This shouldn't be executed")
End If
%>
The explanation is the same as above, but with only one detail:
ElseIf Verify another value passed and check if is true or false
MyName <> MyMiddleName this will check if MyName is different(<>) from MyMiddleName, which is true.
The example above can also be written like this:
<%
Dim MyName, MySurname
MyName = "Ramiro"
MyMiddleName = "Varandas"
MySurname = "Junior"
If MyName = "Junior" Then
Response.Write("This is false")
ElseIf MyName <> "Varandas" Then
Response.Write("This is true")
Else
Response.Write("This won't execute")
End If
%>
Let's see now the loop WHILE:
While Executes the code until the value passed is false, so this means that will execute the code while the value is true.
<%
Dim Start, Finish
Start = 0
Finish = 10
While Start <= Finish
Response.Write("The Start Number is actual: " & Start & " and the Finish number still in: " & Finish & "<br>")
Start = Start + 1
Wend
%>
Explaining the code:
<% opens the ASP code
Dim declares a variable or an array
While it's the begin of the loop and will check if the value passed, in this case. if Start(0) is lower or equal to Finish(10) and will validate it true until Start is bigger than 10.
Response.Write sends a response to the browser
Start = Start + 1 Here, I'm getting the variable Start then I'm assign the value to it as the same value of the Start but I'm increasing this value in one and the value of Start will increase every time that the code get executed, but the assign of the variable to it self won't be 0 that is the original value declared above, but will be the last value of Start.
Wend says to the script to return at While and check if is true or false the code, if true, the code will be executed again, if false, the script will go to the first code below Wend
%> closes the ASP code.
While is very useful when looping through database records, that I'll show how to do it in the next part of the article, that will be Connecting to the database and looping through records.
Let's see now the FOR loop.
FOR check a value passed and execute the code if true or false.
Using the above example, let's make it with FOR:
<%
Dim Start, Finish
Finish = 10
For Start = 0 To Finish
Response.Write("The Start Number is actual: " & Start & " and the Finish number still in: " & Finish & "<br>")
Next
%>
Explaining the code:
<% open the ASP code
Dim declares a variable
For begins the loop and check to see if a value is true or false. Otherwise While, the For syntax must be: For variable = number_value To variable or number_value
Response.Write send a response to the browser
Next make the script go back to the FOR statement
%> close the ASP code
Now let's see the Do...LOOP
DO...LOOP executes a code until the condition (value) passed becomes true.
Let's see an example:
<%
Dim Start
Start = 0
Do Until Start = 10
Response.Write("Start is now: " & Start & "<br>")
Start = Start + 1
Loop
%>
Explaining the code:
<% opens the ASP code
Dim declares a variable or an array
Do Until begins the loop and test the value. If the value is false, the code is executed, if is true, the loop exit.
Response.Write send a response to the browser
Start = Start + 1 increment the variable Start in one
Loop return to the begin of the loop to check the condition
%> closes the ASP code
We'll, as you could see in this part of the tutorial, making loops in ASP isn't
a difficult thing.
Tip: don't try to use IF inside a WHILE loop, because it won't execute. It isn't
like in PHP that you can use the break command.
Next, in Part V: databases, how to connect, check which provider to use and how to use SQL to make a query.