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


Making the ASP Page Smart

July 31, 2000

We have got a web page, a simple form and some ASP code to store any submitted data in variables, but sadly, our page is a page of very little brain. Everytime the page is called from the browser it executes the ASP whether the user presses submit or not; and, while it will not cause problems as-is, as soon as we add code that actually does something with those variables, things will get messy. Just look at how ugly ASP error messages are:


Microsoft VBScript compilation error '800a03f9'

All right, all right, you caught me, I am exaggerating a bit. The error messages are not that bad, they usually include a line number and some frighteningly intelligent help message. *grin*

Moving on, I think the reader will agree that we should avoid those crazy errors altogether. In order to ensure that our web page does not execute the form handling code without submitting the form we can use a very simple conditional test using if...then logic. We will test for a special "hidden" form field that we add to our form like so:


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

	<!--Existing form code-->

	First Name:

	<input type="text" size="30"

	maxlength="50" name="First_Name">

	<br>

	Last Name:

	<input type="text" size="30"

	maxlength="50" name="Last_Name">

	<br>



	<!--Insert new form field-->

	<input type="hidden" name="isSubmitted" value="yes">



	<!--Finish off form with the rest of the exisiting code-->

	<input type="submit" value="Submit Form">

</form>

If you are not familiar with hidden form fields, do not worry, they are really self-explanatory. Basically they are form fields hidden from the user's view in that they do not show up on the web page but are submitted with every other field and are given the value you, as the designer, assign when the page is created. Easy, no?

For our purposes, the hidden form field is named descriptively as "isSubmitted" with an assigned value of "yes." Here we come to the hardest part of making our ASP smart - the ASP! However, you will quickly see that it is considerably less difficult then you may be expecting.

To begin, we need to revisit our existing ASP code:


<%

	Dim fname, lname



	fname = Request.Querystring("First_Name")

	lname = Request.Querystring("Last_Name")



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

%>>

Now, to add some conditional testing all we need to do is take this existing code and put it inside a simple if...then statement like so:

<%

	if [some condition is true] then



		Dim fname, lname



		fname = Request.Querystring("First_Name")

		lname = Request.Querystring("Last_Name")



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

	end if

%>

All that is missing is the condition itself (indicated above as [some condition is true] above), which is equally easy thanks to the QueryString Collection that we discussed before. Let's walk through this one together so there are no questions in anyone's mind about how the condition is set up. First, what variable are we testing for? Every conditional test has to have a variable to test and an anticipated result that is either determined to be true or false in each case. How about Request.Querystring("isSubmitted") = "yes"?

TIP: If you are relatively new to VBScript (the language driving the ASP), then you should note that it is syntactically different from JavaScript. For instance, if you were coding an if...then statement in JavaScript then you would type:
if (condition is true) { processing... processing... } In VBScript, you have to remember to include then and end if as illustrated above. If you leave these things out your code will not work properly.

In this case our test variable is the isSubmitted form field as it is stored in the querystring after submission. The anticipated result is that this condition will be true and then execute whatever code follows the then part of the if statement. If the condition is false (if Request.Querystring("isSubmitted") does not equal "yes" or is empty (which occurs only when the form is not submitted), then the ASP form handling code will not be executed.

At this point, you should be starting to see the potential of ASP form handling. For example, you can check the validity of form fields with server-side ASP instead of using client-side JavaScript. If the user has a browser that does not support JavaScript or they have it turned off, your ASP web page will still be smart enough to validate their submitted data.

Another possible use for the skills obtained through this tutorial would be to create individual portions of a web page dynamically, as the user is actually using the page. For instance, when I was working on Carcruises.com, the owner asked if we could have a more intuitive way of finding car cruises. So, we developed a system wherein the user picks the state for which they want to locate a cruise. Then, the ASP form handling code matches the selected state to the corresponding months available in the database using an SQL query. Next, the ASP writes a new select box to the page that lists the results of the query. Not only is this method more functional, it leaves room to make web pages that are incredibly intelligent. To top it off, it was all done using the techniques we have been discussing (and a little SQL of course).

Adding the ASP Code
Using ASP for Form Handling
Working with CDONTS


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