|
| | Submitted on: 10/15/2003 12:42:54 PM
By: Travis Howle
Level: Intermediate User Rating:
By 12 Users Compatibility:ASP (Active Server Pages), VbScript (browser/client side)
Users have accessed this article 3100 times. | (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 | | | Your Vote! |
See Voting Log | | Other User Comments | 10/15/2003 12:50:41 PM:Travis Howle I hope you like 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'
| 10/16/2003 12:50:44 PM:Brian 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.
| 10/16/2003 5:37:26 PM:Travis 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?
| 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.
| 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.
| 10/20/2003 8:42:09 AM:Brian 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...
| 11/20/2003 7:00:03 AM:mackeyrj 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.
| 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...
| 11/20/2003 9:39:32 AM: Silly!
| | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |