A few cool ASP Tricks!

Submitted on: 1/6/2015 10:53:00 PM
By: Travis Howle (from psc cd)  
Level: Intermediate
User Rating: By 17 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 1954
     This will teach the users a little of the cool ASP tricks. Nothing to do with a database really, but They are still kinda cool just to test with!

				Forms are great for collecting and handling data on your website. But how can you use ASP to make the most of your forms? Most webhosts provide standard scripts for form handling, but to make your site truly dynamic, it will not be long before you want forms to do something more than the average generic script offers.

In this tutorial, we will look at how we can collect and begin to process data from submitted forms on your website. Let`s start with a basic form (say call the file form.asp) as below with two fields, name and email address:

< form method="post" action="formhandler.asp" < br>name="whateveryouwant">< br> Name: < input type="text" name="name">< br>
Email address: < input type="text" name="email">
< input type="submit" name="Submit" value="Submit">
< /form>

Now let`s begin to create formhandler.asp. The input from the form field called "name" is retrieved by calling request.form("name") and for the email form field, request.form("email"). Simple?

The first thing to consider is, what happens if someone leaves the fields blank. I imagine you don`t want to let them do that, so let`s set up a basic statement to handle that. We will be using If - Then - Else which is talked about in a later tutorial:

< %
If request.form("name")="" or request.form
("email")="" then
response.redirect "form.asp"
End if
% >

OK, so if either the name or email field were left blank, nothing is processed and the user is redirected back to the form to try again. In a later tutorial we may show you a nice way to highlight the fields that are required.

Right lets assume the user had the wit to actually input their name and email address. So what can you do with it? There are various possibilities: you could store the information in a database (more later on that), you could email it to yourself (again more later on this) or you could simply output the data on the webpage....ok that`s not very useful, but for the purposes of this basic tutorial it is what we will do.

So if you want to test this, set up your form.asp using the form code in the first codebox and set up formhandler.asp as follows:

< %
If request.form("name")="" or request.form
("email")="" then
response.redirect "form.asp"
End if

response.write "Name: " & request.form("name")
& "
Email: " & request.form("email")
% >

So simply, if both fields are filled in, it will output the Name and Email address. Note how you can combine text and ASP variables in your response.write statement as above.


There are several different ways of displaying dates.
You can also add and subtract from them as well. I used one (1) in the examples below but, you can use any number you like.

Date 10/15/2003
< %=Date%>

Long Date Wednesday, October 15, 2003
< %=FormatDateTime(Now(),vbLongDate)%>

Adding to Long Date Thursday, October 16, 2003
< %=FormatDateTime(Now()+1,vbLongDate)%>

Subtracting From Long Date Tuesday, October 14, 2003
< %=FormatDateTime(Now()-1,vbLongDate)%>

Short Date 10/15/2003
< %=FormatDateTime(Now(),vbShortDate)%>

Adding To Short Date 10/16/2003
< %=FormatDateTime(Now()+1,vbShortDate)%>

Subtracting From Short Date Tuesday, October 14, 2003
< %=FormatDateTime(Now()-1,vbShortDate)%>

General Date 10/15/2003 4:32:27 PM
< %=FormatDateTime(Now(),vbGeneralDate)%>

Adding To General Date 10/16/2003 4:32:27 PM < %=FormatDateTime(Now()+1,vbGeneralDate)%>

Subtracting From General Date 10/14/2003 4:32:27 PM
< %=FormatDateTime(Now()-1,vbGeneralDate)%>


To see if a specific file exists on the web server, then show the results to the user use the following code in an ASP page:

< %
Set fs=Server.CreateObject

If (fs.FileExists("c:\inetpub\www\default.asp"))=true Then
c:\winnt\cursors\The DEFUALT.asp File exists.") Else
Response.Write("File c:\winnt\cursors\The File does not exist.")
End If

set fs=nothing
% >


To check to see if a drive is ready. Such as drive D for a CD copy or something type:

< %
dim fs,d,n
set fs=Server.CreateObject
set d=fs.GetDrive("c:")
n = "The " & d.DriveLetter
if d.IsReady=true then
n = n & " drive is ready."
n = n & " drive is not ready."
end if
set d=nothing
set fs=nothing
% >

I hope you liked it! And I hope it helped you :D Pleaes remember to vote! lol ;)

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.