Important alert: (current site time 7/15/2013 10:19:54 PM EDT)
 

article

A few cool ASP Tricks!

Email
Submitted on: 10/15/2003 12:42:54 PM
By: Travis Howle  
Level: Intermediate
User Rating: By 17 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 24074
author picture
(About the author)
 
     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!


 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				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"
response.end
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"
response.end
End if

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

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
("Scripting.FileSystemObject")

If (fs.FileExists("c:\inetpub\www\default.asp"))=true Then
Response.Write("File
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
("Scripting.FileSystemObject")
set d=fs.GetDrive("c:")
n = "The " & d.DriveLetter
if d.IsReady=true then
n = n & " drive is ready."
else
n = n & " drive is not ready."
end if
Response.Write(n)
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

10/15/2003 12:50:41 PMTravis Howle

I hope you like it :$
(If this comment was disrespectful, please report it.)

 
10/16/2003 9:42:16 AM

I'm sorry, but these aren't tricks. These are basic things that anybody can find in a 'Learn asp" type book. It isn't intermidiate either.. try beginner. You don't ever cover server.mapppath since most users don't know the full path to their website on the webserver. Finally, you say this is client side compatible, which it isn't. Client-side vbscript doesn't understand 'Server.CreateObject'
(If this comment was disrespectful, please report it.)

 
10/16/2003 12:50:44 PMBrian Battles WS1O

Sorry, but these are NOT "cool ASP tricks," they're just very simple techniques that all ASP programmers should learn right at the start. Definitely beginning-level stuff, not especially fresh or unique. Nice of you to make the effort, but this material is easily found in many other places. Also, be careful of your spelling (eg, "DEFUALT," "plaes," etc. It makes you look amateurish.
(If this comment was disrespectful, please report it.)

 
10/16/2003 5:37:26 PMTravis Howle

Sorry then. Ill try ot make something better that will get a better score of votes.. Wait.. Lets ask you. What do you think I should post, What do you want ot know?
(If this comment was disrespectful, please report it.)

 
10/16/2003 5:53:31 PM

Travis, thanks, I found a couple things in your article to add to my repertoire. Thanks for taking the time to give back to the asp community!!! (BTW, to Travis' critics: I have a couple of very popular and "important" websites and I've been asp coding for a couple of years now. Sometimes, finding tips on the "basic" things are the toughest because everyone assumes that everyone else already knows all that.
(If this comment was disrespectful, please report it.)

 
10/17/2003 10:13:01 AM

(I had my name on that last one, not sure why it didn't show up - Brian)
I'm fine with people posting the basics and I agree that they can be useful. But if that's what they are posting then that is what it should be called, not intermidiate 'tricks'.

Here's one I've been looking for.. how about authenticating someone against a domain controller from a custom login screen.. NOT using IIS's login pop-up AND getting the proper credentials so that the user then has rights to the appropriate files on the server based on their login information.
(If this comment was disrespectful, please report it.)

 
10/20/2003 8:42:09 AMBrian Battles WS1O

Travis, some handy stuff for beginners might include showing them how to send e-mail with CDONTS or CDOSYS, how to send Server.QueryString data from one page to another, showing how to use form fields, the difference between POST and GET, etc. Just a few suggestions...
(If this comment was disrespectful, please report it.)

 
11/20/2003 7:00:03 AMmackeyrj

I agree with the crowd. The info is not at the intermidiate level, but the info is very useful for beginners. Don't feel too discouraged to continue making submissions. Just be be critical of the level they are submitted to.
(If this comment was disrespectful, please report it.)

 
11/20/2003 9:33:47 AM

All stop! This tutorial is good and sholud be a very good remainder to all asp programmers of simplicity and good programming skills. So donīt be nasty...
(If this comment was disrespectful, please report it.)

 
11/20/2003 9:39:32 AM

Silly!
(If this comment was disrespectful, please report it.)

 
11/20/2003 9:23:12 PM

I believe that this article lived up to its title... A few cool ASP Tricks! I have been coding for over 10 years in languages from Xbase, VB, basic, VBscript, Javascript, PHP, SQL, ASP, and .Net. Not bragging, because I never really had time to become proficient at any of them. I am always delighted to find some code snippets that will make my job easier. Furthermore, I find the remarks above childish.
(If this comment was disrespectful, please report it.)

 
4/20/2004 1:43:53 PMHTML Dude

If any of the critics think they can do better, let them post a link to their material. To me actions always speak louder than words.

I for one found it useful (we all make spelling mistakes) and the experience gained by noting how certain errors in scripts are created, helps (I think) to make us better programmers.

Thanks, I particularly found the date section useful.
(If this comment was disrespectful, please report it.)

 
5/7/2004 7:16:50 PM

I have no clue what that code is about. I don't go to school to learn this stuff. But to me it's people like travis who make it possible for people like me to learn. Thank's travis keep helping the coding community.
(If this comment was disrespectful, please report it.)

 
8/3/2004 5:46:32 PM

contribution is good thing what ever it is ...

(If this comment was disrespectful, please report it.)

 
8/3/2004 10:58:56 PMTravis Howle

Well, It's been a while since I posted this.. A long while. I am sorry for those of you that don't like my postings (I look back on it, and actually agree with you to a point). And I want to think you all that have made comments about helping the community and stuff. I've learned more over the past.. How long has it been? Few years! And I'll be making some more posts that actually will help the community.

Thanks yall,
Travis
(If this comment was disrespectful, please report it.)

 

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.