| << 3.2.2- Output to the User | Chapter3 | 3.3.0- Summary >> |
A Simple Business Example
Before we head on to the in-depth discussionsin the rest of the book, I suggest you give these first techniques a few tries.Here is a simple business situation with which we can exercise our newtechniques. The solution requires two files: the first contains an HTML formand the second is an ASP page that responds to the information submitted in the form, utilizing the data entered to return a customized page to theuser. Once you are comfortable with the ASP projects in this chapter, you willbe able to incorporate the scores of improvements that the rest of this bookpresents.
Try It Out – Sign-In Sheet Example
You travel to various offices around the world, presentingseminars to your colleagues on the new products that your company is about tointroduce. Paris in April, Tokyo in September; it is a very glamorous job. Butyou do have some paperwork. At each seminar you need to make a sign-in sheetwith the company logo, date, and so on; and a number of horizontal lines thateach of the delegates of your seminar must sign to prove that they attended theseminar.
You want to make up a template of the sign-in sheet on yourweb site. The idea is that, wherever you are in the world, you can browse toyour web server back home, type in the location and date of the seminar, andthen have the web server create a sign-in sheet on the browser (with thelocation and date information written in). Then you can just print the pagefrom the browser and have a beautiful sign-in sheet.
For this example, pick any piece of clipart for your companylogo.
1. You've guessed it…crank up your trusty web page editor andtype in the following:
<HTML>
<HEAD>
<TITLE>'Where Am I'form for Sign-in Sheets</TITLE>
</HEAD>
<BODY>
<H1>Sign-in Sheettemplate for my 'New Products' Seminars</H1>
<BR>
Just fill in the followingdetails:
<FORMACTION="SignInSheet.asp" METHOD=Post>
<P>City:<INPUTTYPE="TEXT" Name="City"></P>
<P>Date:<INPUTTYPE="TEXT" Name="Date"></P>
<P><INPUTTYPE="SUBMIT" VALUE="Click here to submit theinformation"></P>
<P><INPUTTYPE="RESET" VALUE="Click here to start over"></P>
</FORM>
</BODY>
</HTML>
2. Save this file as WhereAmI.asp, into your \inetpub\wwwroot\BegASPFilesdirectory.
3. Close this page down and start another new one; type thefollowing into it:
<HTML>
<HEAD>
<TITLE>Sign In Sheetfor New Products Seminar</TITLE>
</HEAD>
<BODY>
<H1>On-Line Clothiers<IMG SRC="Bizrun.jpg" WIDTH="105" HEIGHT="111"></H1>
<H1>Welcome to the NewProducts Seminar</H1>
<%
Dim strCity, strDate
strDate =Request.Form("Date")
strCity =Request.Form("City")
Response.Write "Heldin "
Response.Write strCity
Response.Write " on"
Response.Write strDate
%>
<PALIGN="left">please sign in by printing your name</P>
<HR>
 <HR>
 <HR>
 <HR>
</BODY>
</HTML>
4. Savethis file as SignInSheet.asp,in the same directory.
5. Open the WhereAmI.asppage in your browser, type in a city and a date and click on the Submit button:
|
|
6. If you submitted the name Beijing,and the date December 16, 1999, then you'd seethe following:
How It Works
The solution is contained in two pages. The first is a pagethat doesn't use ASP but does contain a form with input fields for the user tosubmit data. That submission calls an ASP page, SignInSheet.asp,in which the submitted data is available to be written at appropriate points inthe page.
The form page (code for the body shown below) is notdissimilar to things we've seen in the other exercises in this chapter. The <FORM> tag must always have the action attribute– in this case pointing to the page named SignInSheet.asp.There are two input fields in this form, with the names Cityand Date, each reflecting the type of informationexpected and each very important in allowing us to distinguish the differentpieces of information in SignInSheet.asp:
<H1>Sign-in Sheet template for my 'New Products'Seminars</H1>
<BR>
Just fill in the following details:
<FORM ACTION="SignInSheet.asp" METHOD=POST
<P>City:<INPUT TYPE="TEXT"VALUE="Name of City" Name="City"></P>
<P>Date:<INPUT TYPE="TEXT"VALUE="Date of Seminar" Name="Date"></P>
<P><INPUT TYPE="SUBMIT"VALUE="Click here to submit the information"></P>
<P><INPUT TYPE="RESET"VALUE="Click here to start over"></P>
</FORM>
SignInSheet.asp contains theASP that handles this input, and it's where the action happens. The objectiveof this page is to take the sign-in sheet template that we've designed, and addin the city and date details for this particular seminar (as the userentered in the first page). When the web server has done that, and has sent theresult to the browser, we can print out the page from the browser and hand itround the lecture room during the seminar.
After the normal <HEAD>material, the <BODY> begins. Two headinglevel 1 lines splash the company name and logo across the top of the page,followed by a Welcome… line:
<BODY>
<H1>On-Line Clothiers <IMGSRC="Bizrun.jpg" WIDTH="105"HEIGHT="111"></H1>
<H1>Welcome to the New ProductsSeminar</H1>
In the next line we use <%to shift gears into ASP. The first line of script creates two a variables,named strCity and strDate:
<%
Dim strCity, strDate
We use these two variables to hold the values that the usersubmitted to this page, via the form:
strDate = Request.Form("Date")
strCity = Request.Form("City")
So we haven't done any Response.Write-ingyet, but we have taken the time to prepare for it – by setting up allour variables in advance. This makes our code nice and tidy.
Now we use a sequence of Response.Writelines to write the location and date information to the page. There are four Response.Write lines in total, alternately writing alittle plain text and then a variable value:
Response.Write "Held in "
Response.Write strCity
Response.Write " on "
Response.Write strDate
%>
The closing %> delimitermarks the end of the ASP. Then we write some more text and HTML to the page, togenerate a few lines for the delegates to write on:
<P ALIGN="left">please sign in byprinting your name</P>
<HR>
 <HR>
 <HR>
 <HR>
</BODY>
</HTML>
An Alternative Version of The Code
If you'd wanted to use the shortcut for Response.Write, then the code in SignInSheet.aspwould have contained a block of code like this:
<%
Dim strCity, strDate
strCity = Request.Form("City")
strDate = Request.Form("Date")
%>
Held in <%=strCity%> on <%= strDate%>
Once again, note that the <%=… %> shortcut is a very useful syntax, when you need to use ASP to drop variable values into HTML output like this. Remember that each <%… %> or <%= …%> pair means another trip to the ASP DLL,which means that your ASP page will take a little longer to execute. If youonly use it occasionally then it can be a boost to your code's readability,with no significant loss in performance. But if you use it very many times,then you will start to notice slower delivery times for your web pages.
This finishes our basic ASP examples. You might not haveunderstood all of the working behind the code, but you should at least have anidea of how to send information in a form and display it on a separate page.
| << 3.2.2- Output to the User | Chapter3 | 3.3.0- Summary >> |

RSS



