Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

ASP Subroutines

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

robinson
Newbie


Posts: 18
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 0 (Donate)
Post: #1

Wink ASP Subroutines


Subroutines have the same utility in ASP as it has in other languages.

In the next two examples, we have asked our visitors his name, and depending on that answer a different response is sent to the client. The response will be the same in both cases, but in the second one subroutines are used. The use of subroutines may be very useful when there are a lot of instructions to be perform within a subroutine. This way it will allow us to simplify the structure of our script.

Example 1

Code:
<%
TheName=request.form("name)

if TheName="John" then
response.write ("Hi, John. How are you?")
response.write ("<br>Did you know I got married last month?")
else
response.write ("Hi. How are you?")
end if
%>



Example 2

Code:
<%
TheName=request.form("name)

if TheName="John" then
ResponseToJohn()
else
ResponseToUnknown()
end if

Sub ResponseToJohn()
response.write ("Hi, John. How are you?")
response.write ("<br>Did you know I got married last month?")
End Sub

Sub ResponseToUnknown()
response.write ("Hi. How are you?")
End Sub

%>


In order to call a subroutine, we will use this kind of code:

Whatever()

Where Whatever is the name of the subroutine (it is recommended to use a very descriptive name of the task we want to perform within the subroutine to make it easier to understand the script). We may also provide information to the subroutine in order to perform the specified task. The data will be provided this way:

Whatever(data1, data2 ... dataN)

In the following example we will provide different data to a unique subroutine depending on the Name of the person provided throw a form:

Example 3

Code:
<%
TheName=request.form("name)

if TheName="John" then
ResponseToVisitor(35,Sue,New York)
else
   if TheName="Peter" then
   ResponseToVisitor(33,Sally,Los Angeles)
   else
   response.write("Who are you?")
   end if
end if

Sub ResponseToVisitor(AA,BB,CC)
response.write ("I know your are" & AA & "years old, ")
response.write ("you are married to" & BB & ", and")
response.write ("you are living in " & CC)
End Sub
%>



In line 14 it is specified AA is the first variable to be get, BB the second one, and CC the third one. The values for the three variables are provided in the same order in line 5 or line 8.

The example above also shows subroutines are very useful to avoid repeating a specific number of tasks several times within the script, so that the script looks more organized and it is smaller.


Include/virtual

Server Site includes or SSI is a very simple programing language (see a small tutorial) but it also has a very limited number of instructions. We will consider only one option SSI allows us to use within our asp scripts: include/virtual.

In the next example we will use the include option of SSI in a our asp script (response.asp). This command allows as to add a set of instructions from different files (file1.txt and file2.txt bellow) and execute them.

Example 4
response.asp

Code:
<%
TheName=request.form("name)

if TheName="John" then
   %>
   <!--#include virtual="/file1.html" -->
   <% else %>
   <!--#include virtual="/file2.asp" -->
   <%
end if %>



File1.html

Code:
Hi, John.<br>
I know your are 31 years old, you are married to Sue, and you are living in New York.
File2.asp
<%
for i=1 to 3
response.write(Thename & "...<BR>")
next
response.write("Who are you?")
%>



In this case, if the name of the person who is visiting our page is John, then we will respond with file1.html. If not, then we will execute some asp instructions from file2.asp.

The include file must be a text file (.txt, .html, .htm, .shtml, .asp...). Although we have used file1.html and file2.asp, the script will work exactly in the same way with file1.txt and file2.txt (changing the name of the files would have no effect).


By using SSI and asp we may also get a secret page:

secret_page.asp

Code:
<%
UserName=request.form ("username")
Password=request.form("password")
if UserName="myusername" and Password="mypassword" then
%>
<!--#include virtual="/cgi-bin/secret_info.txt" -->
<% else %>
<Form Action=secretpage.asp method=post>
Username:  <input type=text name=username size=15><BR>
Password:  <input type=text name=password size=15><BR>
<input type=Submit Value=Send>
</form>
<% end if %>
secret_info.txt
This is my secret information:<BR>
My name is John.<BR>
My surname is Smith.<BR>
<BR>End of secret information.


In this case it is convenient to save secret_info.txt file in the cgi-bin directory (the .txt file is not accessible by visitors from this directory, but it will be accessible from our top directory).

23-09-2006 01:29 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 29-08-2008, 03:34 PM


Copyright © 2002-2008 MyBB Group
Powered By MyBB