Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Adding the ASP Code

July 31, 2000

Now we have got a nice clean HTML form just crying to be used, but it does not have any action - nowhere to go with submitted data. Next we will add some ASP to make the page more useful.

First, we will begin by adding an action to our form. For this particular form, we are just going to make it the name of our file. However, in many situations you can make it the name of another page depending on what you are doing with the form. Since I named the file formtest.asp, then my action tag will look like the following:


<form action="formtest.asp" method="get" name="Input_Form">

Well, that was pretty easy, but where does the ASP come in? So far this page is like just any old page that you could have done in your sleep. All right then, ask and you shall receive! We are now going to add some ASP code to capture the submitted form data and store them in variables.

To begin with, we need to add a fun little symbol to our page that tells the web server ASP code is coming ahead. For our form, we should put this symbol before any HTML code in our sample page. Take a look:


<%



' Note: A single quote denotes a comment in ASP

' (which is primarily powered by VB), just like // double

' forward slashes denote a comment in JavaScript or C++.



' You guessed it, that little <% starts the ASP part of

' the page and if you are really sharp you will also

' guess that %> ends the ASP part of the page.



%>

<html>

	<head>

		<title>ASP Form Sample</title>

	</head>

	<body>

		Our form code, etc...

	</body>

</html>

All right, put your thinking caps on, this next part is crucial to making your page work properly. Up until now we have not talked about the infamous method attribute of the form tag. For an ASP page, this part of the form is particularly important in that it will dictate how you create the ASP form handling code. If you use the post method, then the server will "post" the submitted form data "behind the scenes." That means the user will not be able to see what happens to the data they entered, but a script properly coded will be able to access and manipulate that data.

On the other hand, if you use the get method, then the server will append the submitted form data at the end of the URL in your browser's address bar. You have seen this before (it looks like a bunch of &formfieldname=inputeddata&formfieldname2=inputeddata at the end of the URL - it is called the querystring); a lot of the search engines use it so that you can bookmark your search results page (something you cannot do when the post method is used).

So which method is best? There is not clear-cut answer to that question, suffice it to say that it depends entirely on your needs and what your form does. Basically, if you want to let the user bookmark the resulting page, use the get method. Otherwise, use the post method.

I am going to briefly show you how to code the ASP for both methods. ASP deals with two fundamental objects (there are more, but we are only dealing with the big two in this particular article): Request and Response. Response has many uses and methods (a method is a function that an object performs), but its primary use is to write text (or HTML) to a web page. Request also has many uses and methods, but it is primarily used for obtaining information from the client. Remember, we are dealing with a server-side scripting environment, so it all makes sense. The server "responds" to a client "request." Got it? If not, I will provide some links to some ASP resources at the end of this page.

With either method, you must use the Request object to get the submitted information. If you decided to use the post method, you will need to use the following code:


<%

	Dim fname, lname



	fname = Request.Form("First_Name")

	lname = Request.Form("Last_Name")



%>

In the above code, we are simply using the Forms collection (or a group of common or related items - namely the fields of the form) of the Request object to get the information that the user submitted on your web page. The syntax for objects and collections or methods is simple and consistent throughout ASP pages (with few exceptions): Object.Collection("ITEM_NAME") or for methods, Object.Method("METHOD_PARAMETERS").

If you chose the get method (which is assumed throughout the rest of this article), you cannot use the Forms collection at all and must use the Querystring collection, which acts exactly the same as the Forms collection except it is simply a collection of data in the querystring. Here is the code:


<%

	Dim fname, lname



	fname = Request.Querystring("First_Name")

	lname = Request.Querystring("Last_Name")

%>

Again, the above code uses the standard Object.Collection syntax as I described before. You will note that in both cases, the Item Name is the same as the name attribute of the form's input tags. That is not an accident. Whether you use the Querystring or Form collection, you will need to reference the name you assigned to the input tags when you created your form.

At this point, I would say we are doing pretty well, but there is one problem: every time someone accesses the page, this ASP code is going to be executed. What if we add some Response code to write a little thank you message, like this:


<%

	Dim fname, lname



	fname = Request.Querystring("First_Name")

	lname = Request.Querystring("Last_Name")



	Response.Write("Thanks for submitting your name!")

%>

Even if the user never submits the form, they are going to see the thank you message and that is no good! What is worse, once we add CDONTS, the server will return a cryptic ASP error - and you want to avoid them, trust me.

On the next page, we will discuss how to avoid this annoying problem using a hidden form field and some simple conditional testing.

ASP and VBScript Links:
15 Seconds (Advanced)
ASP Toolbox
ASP Watch
Learn ASP
MS on ASP Microsoft VBSCript Reference PowerASP
Webmonkey ASP Tutorials

Creating the Form
Using ASP for Form Handling
Making the ASP Page Smart


Up to => Home / Authoring / ASP / FormHandling




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers