Building Web Sites All-in-One For Dummies® (84 page)

BOOK: Building Web Sites All-in-One For Dummies®
3.35Mb size Format: txt, pdf, ePub

3.
Save the document.

Save the document with the filename
formPost.htm
. Leave the document open in Dreamweaver.

4.
Choose File
⇒
New.

Dreamweaver displays the New Document dialog box.

5.
Click Blank Page, and then choose the ASP VBScript option from the Page Type column.

6.
Switch to Code view, create a new line after the

tag, and then enter the following:

<%

dim fname

fname=Request.Form(“fname”)

dim lname

lname=request. Form(“lname”)

%>

Response.Write(“Hello” & “ “ & fname & “ “ & lname & “!
” & “How are you?”)

%>

7.
Save the document.

Save the document as
welcomePost.asp
.

8.
Click the form.htm tab.

Each open document in Dreamweaver has its own tab.

9.
Press F12.

Dreamweaver opens the document in your default browser.

10.
Fill in the form and then click Submit.

The form action calls the ASP page, which uses the variables from the form to greet the visitor. (See Figure 2-9.)

Figure 2-9:
Giving the visitor a hearty hello.

Differences between this scenario and the previous scenario include these:

•
The method in which the data is transmitted:
POST
.
With the
post
method, the information is not displayed in the address window of the user's browser.

•
The manner in which the data is transmitted to the ASP page:
The code
dim fname
declares a new variable. The next line of code sets the value of the variable equal to the data
“fname”
that was posted from the form. The
form
collection of the
Request
object is used to get the information.

•
The manner in which the information is written:
Instead of creating several lines of code using the
Write
method of the
response
object, it's compiled on one line of code. The variables and
text strings
(the text surrounded by quotation marks) are
concatenated
(string-speak for
combined to form a single phrase
). The HTML tag


, which is used to create a line break, is also in quotes. Notice the two spaces surrounded by quotation marks. If these were not in the script, the greeting, first name, and last name would run together.

Introducing VBScript functions

VBScript has some built-in functions that enable you to perform certain tasks. The functions are divided into groups, such as Math Functions, Format Functions, String Functions, and so on.

To demonstrate the power of functions, this section covers using the date/time functions to display the date and time on the server's machine. To add the date and time to an ASP page, complete the following steps:

1.
Open an ASP document to which you want to add the date and time.

Position your cursor where you want to display the date and time.

2.
Switch to Code view.

3.
Enter the following code:

<%

Response.Write(“Today is” & “ “ & (Date()) & “
” & “The Server's local time is” & “ “ &(Time ()))

%>

4.
Press F12.

Dreamweaver prompts you to save the page. After you save the page, it's displayed in your default browser. Figure 2-10 shows the date and time added to an ASP page.

When you use the VBScript
Date
function on an ASP page, it gets the date from the server's computer, not the user's computer. However, this is still useful information, especially if your client has a Web site in a different time zone than that of the visitors. You can incorporate the date and time in the greeting; for example,
The date and time in London, England is. . . .

Figure 2-10:
ASP can tell time.

You also have formatting options when you add the
Date
function to a script. The default formatting displays the short version of the date showing the month, day, and year in this format: mm/dd/yyyy. Table 2-1 shows the formatting you can use for the
Date
function.

Table 2-1 Formatting the Date and Time

Constant

Value

Description

vbGeneralDate

0

Display a date in format: mm/dd/yy. If the date parameter is
Now()
, it also returns the time, after the date.

vbLongDate

1

Display a date using the long date format: weekday, month day, year.

vbShortDate

2

Display a date using the short date format, like the default (mm/dd/yy).

vbLongTime

3

Display a time using the time format: hh:mm:ss PM/AM.

vbShortTime

4

Display a time using the 24-hour format: hh:mm.

By adding either a constant that you previously defined and given a value or the value, you can format the date and time. For example, change the code used in the previous example to

<%

Response.Write(“Today is” & “ “ & (FormatDateTime (Date(),1)) & “
” & “The Server's local time is” & “ “ &(Time ()))

%>

Figure 2-11 shows the result. The day of the week and month are spelled out.

Figure 2-11:
The day and month displayed in the long date format.

Discovering Cool ASP Tricks

If you've read this chapter from the start, you know that you can do a lot of things with ASP. The previous sections show you how to do some useful things with ASP; now it's time to take it to the next level. The upcoming sections show you how to add some cool features to your designs using ASP.

Displaying a random image

If you have a client (say a photographer) who wants to display a different image each time a page loads, read on. VBScript can generate random numbers. ASP pages are generated when the browser loads the page. Combine the two, and you can easily display a different image every time the page loads. To create a page that displays a random image each time the page loads, complete the following steps:

1.
Rename the images you want to display randomly, adding a sequence of number suffixes, starting with 1.

We recommend using Adobe Bridge to rename the photos to
IMG_
followed by a number. You end up with a folder of images named
IMG_1.jpg
,
IMG_2.jpg
, and so on.

2.
Open the ASP document in which you want to display a random image each time the page loads.

3.
Position your cursor where you want the image to appear.

To constrain the position of the image, put the VB code in a table row.

4.
Enter the following code:

<%

RANDOMIZE

Dim RndNum

RndNum = Int(30 * Rnd) +1

%>

.jpg”>

That's not a lot of code, but what it achieves is powerful:

• The first line of code,
<%
, tells IIS that this is a script.

• The first statement,
RANDOMIZE
, generates the
Rnd
function's capability to create a random number.

• The third line of code declares a variable called
RndNum
, which is the shortcut for random number.

• The fourth line of code sets the value of the variable.
Int
is a function that generates an
integer,
which you might remember is a whole number.

• The computation in the parentheses takes the number of images, which in this case is
30
, and multiplies that by the
Rnd
function. This generates an integer between
0
and
29
; therefore, a value of
1
is added, so the range is
1
to
30
.

• The fifth line of code,
%>
, ends the VBScript.

• The

tag has the VBScript code
<% =RndNum %>
, which appends
IMG_
with the randomly generated number. If your client has a fairly large number of images, say ten or more, chances are his visitors will see a different image each time they visit the site.

Other books

Martha by Diana Wallis Taylor
Son of Thunder by Libby Bishop
Invisible Influence by Jonah Berger
The Warden by Madeleine Roux
Two Dates Max by Jane, Missy
Say the Word by Julie Johnson
Empire Rising by Rick Campbell