Now that you've learned some of the important fundamentals of VBScript programming, it's time to see how you can put these skills and techniques to practical use. This is the first of several days this week that will teach you how to use scripting elements. Scripting elements reside within an HTML document. They include intrinsic HTML controls and objects that you can insert into HTML such as ActiveX controls. Controls and objects are entities you can place on a Web page to provide an interface to the user that enables him to interact with the page. You can also use them as the front ends to perform behind-the-scenes operations, such as submitting information to a program on a server.
Scripting elements reside within a Web page and include both intrinsic HTML controls and objects such as ActiveX Controls.
Controls and objects are elements you can include in a Web page that allow the user to interface with the page or perform some specific task from within the HTML document.
In today and tomorrow's lessons, you will be introduced to the first type of scripting element-intrinsic HTML controls. Then, in Days 10, "An Introduction to Objects and ActiveX Controls," 11, "More ActiveX Controls," and 12, "Advanced Objects: ActiveX, Java, and ActiveVRML," you will learn more about controls and objects, including ActiveX controls. Before we discuss scripting elements, however, you must first become familiar with HTML forms. This lesson begins by introducing you to HTML forms and discussing their importance. Then, you will learn about three intrinsic HTML controls-the button control, the text control, and the textarea control. In each case, you will learn how to create the control, place it on the form, and interact with it. Today's lesson gives you the basics of what you need to know to work with HTML controls and VBScript. Tomorrow, you will be presented with three more useful HTML controls: the radio button, the check box, and the select control.
The term intrinsic is included in the title of this lesson because the HTML language automatically supports intrinsic HTML controls; the controls are an inherent part of HTML. On Day 10, when you begin to explore ActiveX controls, you will see that ActiveX controls are not inherent to HTML and must be specifically inserted into an HTML document. Furthermore, ActiveX controls are designed for easy component integration into languages such as VBScript. Intrinsic HTML controls, on the other hand, have been around since before scripting languages such as VBScript and components such as ActiveX controls came to be. In their original form in HTML, intrinsic HTML controls were more commonly known as form input controls.
As you can see by the title of this lesson, intrinsic HTML controls apply to HTML forms. It is necessary to understand what an HTML form is and what its capabilities are in order to appreciate the roles and capabilities of the intrinsic controls themselves. If you're unfamiliar with HTML forms, today's lesson will bring you up to speed.
A Web page can be represented in hierarchical fashion by representing the page as a window. This window contains, among many other entities, the document, which, in turn, contains the form. It is within the form that intrinsic HTML controls were originally designed to work. Forms serve as containers whose information can be sent across the Internet to the server that supplied the Web page to the client.
A form is a container into which controls and objects can be placed. The form can transmit the values of intrinsic controls back to the server for processing in response to selection of a submit button. Even if the controls in a form are not intended to gather data for a server, a form can still provide a convenient grouping of the controls it contains.
The form is particularly useful when data must be collected on a page and then submitted back to a server. An example of such a page would be one that collected survey data and then caused that data to be stored in a central database. You typically submit such data to a server using the HTML form submittal and CGI capabilities to launch a script on the server.
CGI stands for Common Gateway Interface. You can think of it as the protocol by which programs are launched on the server in response to interactions on a Web page. Forms gather input from the user to submit to the server. Before scripting languages such as VBScript, forms, coupled with the server application that processed the data they collected, were the primary method of giving a Web page any intelligence. That is, the gateway application on the server had to do the thinking for the Web page and the data had to be sent to the server first before it could act. The HTML form was the medium used to collect and exchange information between the Web page and the server.
Note |
If you're a Visual Basic 4.0 or VBA programmer, you are probably used to thinking of the form as the whole window your program is based upon. Now that you're in the Web page world, the terminology is different! A form is optional on a Web page, and even when present, it is typically just one part of the larger Web page that the user sees. |
Now you see that HTML forms were really designed for Web pages as a means of collecting information from the user in various controls that can be placed on the form. These controls are the subject of the lessons today and tomorrow. An HTML form is an area of the Web page enclosed by the script tags <FORM> and </FORM>. When you use a form, the controls that appear to the user are usually placed within the form. Information they contain can be sent back to the server when the data is submitted. Keep in mind that you use the Common Gateway Interface, or CGI, with HTML to trigger programs on the server when needed. When using CGI to send data back and forth between the client and the server, the form is the container that houses all the information.
Although controls are intended to be placed within a form, they don't have to be. In all the examples so far in the book, we have been using simple controls such as the command button and text control. You have seen Web pages that use these controls, and not one example used an HTML form. You might be wondering, "If they are called HTML form controls, can they be used outside a form?" The answer is, "Yes." HTML enables you to use its intrinsic controls outside of form tags. The only catch is that CGI cannot pull data out of those controls; only the contents of a form can be submitted. Form declarations provide important CGI details such as how data is to be passed back to the server. As a result, the data contained in input controls cannot be sent to the server using CGI if the controls are defined outside of forms. Except for that restriction, your VBScript code can interact with intrinsic controls even outside of forms.
Often, you will create Web pages that simply "do their own thing" and have no need to send information back to a server. If you've got a Web page that doesn't need to use CGI to submit information to a server, then you don't need to place your controls in a form. It certainly is safer to do so, however, because some older browsers might not be able to present controls on a Web page unless they are contained within form tags. This is due to the fact that before scripting languages such as VBScript arrived on the scene, controls were always used within a form. Some browsers do not support them if they are outside the confines of a form. Of course, the older browsers that do not support non-form controls probably won't support your VBScript code either. Nevertheless, even if a user is using pages with an old browser, you'd still like as much of your program to show through as possible. In this and subsequent lessons, we will generally place controls within form tags for compatibility among various browsers.
How do HTML form controls work inside of a form? To understand this, look at the structure of a form. HTML forms are specified by the start tag <FORM> and end tag </FORM>. A form definition is part of the regular HTML body, not within your script tags. You must place all your form elements within these form tags, and you cannot nest a form within another. The opening tag of a form typically uses two attributes: METHOD and ACTION. These attributes are supplied as part of the definition of the behavior of a form. The METHOD attribute tells the browser the format of the data that will be presented to the server when the form is submitted. You can set METHOD to either GET or POST. Each of these commands sends the data to the server in a different format. If you don't specify the METHOD, the default is GET.
Note |
Today's lesson provides background on CGI communication through forms to put the <FORM> declaration in perspective. It is important to understand what a form is to have a clear picture of intrinsic control definitions. However, it is not the intention here to provide a detailed understanding of all nuances of CGI or related aspects such as GET and POST methods because that is less directly related to VBScript. Refer to a book such as Sams.net Publishing's HTML and CGI Unleashed for more details in those areas. Some aspects of this topic will also be covered in slightly more detail on Day 19, "Dynamic Page Flow and Server Submittal." |
The second attribute, ACTION, tells the browser how to find the script code where you send your data after a user's entry in the form is complete. In other words, it tells the browser where the server script is. You can set the ACTION attribute to the URL of the server script or you can use a relative path. In either case, you must have a CGI script on the server side to handle and process the data. The following tags specify a form where the data will be submitted to a specific Web server script:
<FORM METHOD="GET" ACTION="../cgi-bin/myscript">
</FORM>
This tag tells the browser to send the data to the Web server script that is referenced by the relative path of "../cgi-bin/myscript" and to use the "GET" method to send the data to that server. If you don't plan on submitting any data to a server but still want to use a form to contain your controls, you can just use the following statements:
<FORM>
</FORM>
Make sure all the controls you want to present to the user are within the form. These tags create a form that has no target server, so remember that you should not submit the data contained in the form (using the form's submit method described on Day 19)-it will have no place to go.
With an understanding of forms and the context that controls have with them, you can now focus attention on the controls themselves. You can use HTML form controls independently of CGI. Before scripting languages such as VBScript, submitting data to servers through CGI was very important in order to give your Web page even the simplest intelligence. Now that we have a sophisticated scripting language, however, this restriction can be overcome. Still, CGI is often vital and necessary to send data to a server, such as when ordering products, inquiring about information, and so on.
You will learn much more about communicating with Web servers next week on Day 19. For now, I'll defer that topic and discuss the controls themselves.
The first control I will explore is the button. We have been using buttons throughout the book because they are such an important part of a Web page. Buttons are commonly used to give the user the ability to execute VBScript code that performs some action indicated on the caption of the button. For example, you have seen Web pages used to calculate the pace of a runner. The Pace-Pal Web page used the Display Pace button to trigger some VBScript code that calculated the pace for the user and displayed it on the screen.
To create a button on a Web page, you need to use a special tag called an INPUT tag. The INPUT tag tells the browser you are about to create a control used to get input from the user. The button is just one type of control you can create using the INPUT tag. The actual input could come in the form of text, an indication of whether the user has checked the control, or a recognition of the control by clicking it. In any case, information is being exchanged with the user. The input tag takes at least two attributes: TYPE and NAME.
Tip |
The <INPUT> tag is used to place intrinsic HTML controls on a Web page. |
The TYPE attribute is very important because it tells the browser what type of control you want to create. You use the input tag to create text controls, radio button controls, and check boxes, just to name a few. Right now, because I'm discussing buttons, the TYPE attribute will be set equal to the keyword BUTTON. As you will see in this and the next lesson, other keywords are used for the other control types.
The second attribute that you must supply is the NAME attribute, which gives the control a name. Why would you want to name a control? To work with a control, you have to refer to it, and to do that, you must give it a unique name. The name can be any string as long as it starts with a letter. Make sure the names you assign to your controls are descriptive enough for you to easily tell them apart. There's nothing more frustrating than getting confused over what control is being referenced when you see some cryptic name in your code. The name should be clear and tell you what type of control it is. One recommended convention is to prefix the name of a button with the characters cmd, which stand for command button. This is borrowed from the Windows command button and lets you know the type of control you're dealing with. If you see the expression Test in your code, you might not be sure what type of control it is or even whether it is the name of a control. On the other hand, if you see cmdTest, you know right away that you're dealing with a button control.
Note |
You can use many more standards to help make your code clear, readable, and easy to maintain. It's hard to understand all the recommended standards until you've been exposed to the full VBScript language, so a detailed coverage of all standards is deferred until Day 13, "VBScript Standards and Conventions." In the meantime, I'll describe key standards as I discuss those topics, such as using cmd. |
When you work with button controls, you want to set one more attribute-the VALUE attribute. The VALUE attribute sets the caption of the button. The caption tells the user why the button is there. Make sure the caption of your button is descriptive enough to let the user know what happens if he clicks it.
Here's a simple example of a line of HTML code that creates a button:
<INPUT TYPE="Button" NAME="cmdGetCost" VALUE="Get the Cost">
Here, the name of the control is cmdGetCost and the caption that will appear to the user on the Web page is "Get the Cost."
The size of a command button depends on the length of caption as well as the size of the font. The browser will automatically size the button based on the text you set in the VALUE attribute. The button surrounds the text, but the amount of real estate used to surround the button is beyond your control. The formatting is up to the browser, not HTML, in this case.
A command button appears in a Web page exactly where you put it, much like text or any other element you place within a Web page. For more information on HTML, you might want to refer to Lemay's Teach Yourself Web Publishing with HTML 3.2 in 14 Days, Professional Reference Edition. Otherwise, I'll assume you know how to format an HTML document sufficiently to place a command button where you want. Of course, you can look at the Web pages provided with this book to gain more insight, and you will also be taught more on how to do this on Day 14, "Working with Documents and User Interface Functions." Also keep in mind that buttons other than the submit button are not very useful in simple HTML because you can't use them to respond to the user. They do find usefulness, however, with the advent of VBScript. As you will see in a moment, it is very easy to write VBScript code to respond to the user clicking a button.
Tip |
One commonly used strategy to position input controls such as buttons with a finer degree of detail is to place them within HTML tables. Refer to resources such as Lemay's Teach Yourself Web Publishing with HTML 3.2 in 14 Days, Professional Reference Edition, for more information on HTML table capabilities. |
Listing 8.1 shows a simple example of a Web page that contains a button.
Listing 8.1. A simple Web page with an HTML button control.
<HTML>
<HEAD>
<TITLE>The Button Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Button Control</EM></h1>
<HR>
<P>This Web page demonstrates how to place a simple button control on
a Web page. If you click on the button, nothing happens yet, but the
next program will show the user a message box when they click on this
button.
<P>
<CENTER>
<INPUT TYPE="BUTTON" NAME="cmdBegin" VALUE="Click to Begin">
</CENTER>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
-->
</SCRIPT>
</BODY>
</HTML>
Figure 8.1 shows the corresponding Web page. The Web page with this simple button, named simpbtn.htm, is available on the CD-ROM that accompanies the book.
Figure 8.1 : A simple Web page with a button.
The first thing you should notice is the single input statement that specifies a button control within the form. You will also notice a set of VBScript tags in the document. Right now, there is no code between the tags. As you learn more about the button control, you will place code there as needed.
That's pretty much all you must do to place a button on the screen. You cannot change the dimensions of the button manually, nor can you specify the color of the button or the text that appears in the button. All you can do is place the button in your Web page and give it a name and a caption. A button is pretty worthless, though, if you can't respond to it when the user clicks it. That's where VBScript comes in.
If you were merely using HTML, a button wouldn't do you much good. You wouldn't be able to respond to a button click. In HTML, the only button control the browser responds to is a control that is declared with the type SUBMIT. This places a special button on the form that, when clicked, submits the form within the Web page to the location specified in the ACTION attribute. You will learn more about this on Day 19 when you learn about CGI scripts and VBScript.
How do you go about connecting the button with VBScript code? Quite easily, in fact. On Day 7, "Building a Home for Your Code," you learned about a special procedure called an event procedure. You saw that event procedures are designed to respond to events initiated by the user, usually on controls. To get your VBScript code to respond to a button click, you have two options. First, you can use the implicit event procedure, which has the following format:
Sub ButtonName_OnClick()
...place your code here
End Sub
where ButtonName is the name of the button. You gave the button this name using the NAME property of the input tag when you created it. Consider Listing 8.2, which shows VBScript code that responds to the click of the button by showing the user a message box.
Note |
In HTML terms, the NAME property of the input tag is an attribute. Because you will use it as a property of the control, the term property is used here. |
Listing 8.2. A simple Web page with an HTML button control and VBScript code that responds to the user's click.
<HTML>
<HEAD>
<TITLE>The Button Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Button Control</EM></h1>
<HR>
<CENTER><H2>Using the Implicit Event Procedure</H2></CENTER>
<P>This Web page demonstrates how to place a simple button control on
a Web page. If you click on the button, you will see a message box appear.
<CENTER><INPUT TYPE="BUTTON" NAME="cmdBegin" VALUE="Click to Begin"
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> andÂ
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdBegin_OnClick()
MsgBox "You have clicked on the command button.Â
I knew you couldn't resist!"
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
Figure 8.2 shows the corresponding Web page after the user has clicked on the button. The Web page with this implicit button example, named implbtn.htm, is available on the CD-ROM that accompanies the book.
Figure 8.2 : Using a button and an implicit event procedure.
As you can see, the implicit event procedure is named according to conventions. If you do not name the subroutine properly, it will not connect to the button and will never be called.
The other way of connecting a command button to code is through the explicit event procedure. Rather than create a procedure that uses the naming convention required using the implicit event procedure, in this case Sub buttonname_OnClick(), you can specify the subroutine you want to call, and it can be named anything you like. You do this by using a special attribute called OncLICK when you create the button. All you need to do is set the OncLICK attribute equal to the procedure you want to call when the user clicks the button. Then, you need to actually create the subroutine in your code. Consider the following declaration of a button.
<INPUT TYPE="Button" NAME="cmdGetCost" VALUE="Get the Cost"
LANGUAGE="VBScript" OncLICK="GetCost">
In this example, the attribute OncLICK executes VBScript code that calls the GetCost subroutine. Obviously, this subroutine must exist within the Web page. The benefit of this approach is that you don't need a unique event procedure for each event of a control. You can share a procedure among several controls if you like. Also note that you should set the LANGUAGE attribute to VBScript. If you don't, the browser may not be able to identify what scripting language you are using. It may, for example, assume that you are using JavaScript instead. Therefore, you should always set this attribute to make it clear to the browser what scripting language applies.
To see an example of this, consider the code in Listing 8.3. Here, you can see a Web page that uses this technique.
Listing 8.3. Using a button and an explicit event procedure.
<HTML>
<HEAD>
<TITLE>The Button Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Button Control</EM></h1>
<HR>
<CENTER><H2>Using the Explicit Event Procedure</H2></CENTER>
<P>This Web page demonstrates how to use an explicit event procedure to
call the same procedure when you click on two different buttons. Click
on either button and the same procedure will be called.
<P><INPUT TYPE="BUTTON" NAME="cmdButton1" VALUE="Button One"
LANGUAGE="VBScript" OncLICK="ShowMessage 1">
<P><INPUT TYPE="BUTTON" NAME="cmdButton2" VALUE="Button Two"
LANGUAGE="VBScript" OncLICK="ShowMessage 2">
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub ShowMessage(ButtonValue)
MsgBox "You have clicked on button #" & ButtonValue
End Sub
-->
</SCRIPT>
</BODY></HTML>
Figure 8.3 shows this Web page, which is named explbtn.htm. The Web page for this explicit button sample is also available on the CD-ROM that accompanies the book.
Figure 8.3 : A simple Web page with two buttons that both call the same event procedure explicitly.
Now, either button will call the same procedure when the user clicks it. This can be useful when you want the same procedure to handle different controls on a form. Notice that in the OncLICK attribute, a parameter is passed to the subroutine when the event procedure ShowMessage is called. This parameter tells the subroutine which button the user clicked. Although there is no way to pass the internal representation of the button object to the subroutine, you can pass any literal value that the subroutine can interpret. The subroutine can then look at the value of a string passed to it as a parameter, for example, and tell where it was called from. Using this approach, you can create very flexible procedures to respond to various controls that all call that same procedure. In this way, you can share data among controls and make your programs more flexible.
The OncLICK attribute not only lets you indicate procedures to be activated when the event occurs, it also lets you create script code on the fly in the button definition. This is done by enclosing your code in single quotes immediately after the OncLICK attribute. For example, if you wanted to pop up a message box for the user when he clicks a button without triggering a separate event procedure, you can simply use the following definition for the button:
<INPUT TYPE="BUTTON" NAME="cmdButton1" LANGUAGE="VBScript"
VALUE="Button One" OncLICK='MsgBox "You just clicked on Button #1"' >
This technique is demonstrated in the Web page named onclick.htm on the CD-ROM that accompanies the book. This approach makes it very convenient to throw a bit of code here and there in response to controls, but beware! The more you scatter your code around in attributes of controls, the more difficult it will be to find and debug your code later. It is a good idea to keep your code within procedures and call those procedures using the OncLICK attribute or use the implicit event procedure approach for your controls.
Note |
You can also specify code in the OncLICK event by enclosing it in double quotes, but you need to be aware of a little trick if you have to use double quotes within the script statement. Simply specify two double quotes in a row in the inner statement, and they will be interpreted as one. For example, this doesn't work: <P><INPUT TYPE="BUTTON" NAME="cmdButton1" VALUE="Button One" LANGUAGE="VBScript" OncLICK="MsgBox "You just clicked on Button #1"" > but this does work: <P><INPUT TYPE="BUTTON" NAME="cmdButton1" VALUE="Button One" LANGUAGE="VBScript" OncLICK="MsgBox ""You just clicked on Button #1"" " > This same rule applies to all VBScript code inside or outside script procedures. |
Another interesting feature of VBScript is the capability to create a special block of script code that responds to a control event without placing it within a procedure at all! This can be accomplished using the EVENT and FOR attributes in an opening script tag. The EVENT attribute is assigned a string indicating the event that the script is for, and the FOR attribute is assigned to the control that the script applies to. To write a script that presents a message box for the button cmdButton1, you could create the following script:
<SCRIPT LANGUAGE="VBScript" EVENT="OncLICK" FOR="cmdButton1">
<!--
MsgBox "You just clicked on button #1"
-->
</SCRIPT>
In this case, the entire script is for that specific event. You don't need to create a procedure because the entire script is devoted to the control and event you specify. Usually, any script statements between the <SCRIPT> and </SCRIPT> tags that are not enclosed in procedures are executed as soon as the page loads into the browser. If you use the EVENT attribute, however, this is not the case. The only time these statements of code are ever executed is when the event occurs. This can be a useful capability. You can declare several different scripts within the same page, and data declared in each script can be accessed by the other scripts. You could build a good event-handling approach by dedicating a separate script (enclosed in <SCRIPT> and </SCRIPT> tags) for each event instead of creating a separate procedure for each event. For purposes of clarity, using procedures within the same script might be easier. Then, all your information is in one place.
To see an example of this, consider the Web page eventfor.htm on the CD-ROM that accompanies this book. The Web page is shown in Figure 8.4.
Figure 8.4 : A simple Web page with two buttons that increment a counter.
Listing 8.4 shows the code for the page.
Listing 8.4. Using the EVENT and FOR attributes of a script to trigger a control event.
<HTML>
<HEAD>
<TITLE>Creating Event Procedures</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>Creating Event Procedures</EM></h1>
<HR>
<CENTER><H2>Using EVENT and FOR to Launch Script Events</H2></CENTER>
<P>This Web page uses the EVENT and FOR attributes of a script to
define the script as an event procedure for a specific control.
<P><INPUT TYPE="BUTTON" NAME="cmdButton1" VALUE="Button One">
<P><INPUT TYPE="BUTTON" NAME="cmdButton2" VALUE="Button Two">
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
Dim Counter
-->
</SCRIPT>
<SCRIPT LANGUAGE="VBScript" EVENT="OncLICK" FOR="cmdButton1">
<!--
Counter = Counter + 1
MsgBox "Button #1 has just incremented the Counter to " & Counter
-->
</SCRIPT>
<SCRIPT LANGUAGE="VBScript" EVENT="OncLICK" FOR="cmdButton2">
<!--
Counter = Counter + 1
MsgBox "Button #2 has just incremented the Counter to " & Counter
-->
</SCRIPT>
</BODY>
</HTML>
This example has two command buttons on the Web page. Each button has its own associated OnClick event procedure that displays a message box on the screen. Each script has access to a script-level variable named Counter that each script increments. The message box for each button displays the value of the counter variable. This demonstrates the usefulness of the EVENT and FOR attributes in a script.
In the examples shown this far, the command button's OnClick event was executed automatically whenever the user clicked on the button. Suppose, however, that you want to place the focus on a command button in code rather than by user intervention. To do this, you need to execute a special instruction called a method. Methods are statements associated with objects and controls that cause some action to be carried out. You call a method associated with a control using the following syntax:
formname.controlname.methodname
where formname is the name of the form, controlname is the name of the control, and methodname is the name of the method. At the time this book went to print, you had to create and use the form in order to work with control methods.
A method is an instruction associated with a control that causes some activity to occur within the control. One way that methods are used in VBScript is to cause control events to occur from code rather than as the result of direct user interaction with the control.
In the case of the button, if you want to simulate the user's clicking on the button, you simply call
formname.buttonname.Click
where formname is the name of the form and buttonname is the name of the button control. Notice that you must create and use a form in order to use methods in VBScript. Consider the Web page named click.htm, which is shown in Figure 8.5.
Figure 8.5 : A Web page that uses the Click method of a button.
The code for this Web page is shown in Listing 8.5.
Listing 8.5. Using the Click method of a button control.
<HTML>
<HEAD>
<TITLE>The Button Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Button Control</EM></h1>
<HR>
<CENTER><H2>Using the OnClick event and Click Method</H2></CENTER>
<P>This Web page demonstrates how to use the OnClick event and Click method
to take action on and simulate button clicks.
<FORM NAME="MyForm">
<P><INPUT TYPE="BUTTON" NAME="cmdButton1" VALUE="Button One" >
<P><INPUT TYPE="BUTTON" NAME="cmdButton2" VALUE="Button Two" >
</FORM>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdButton1_OnClick()
MsgBox "You have clicked on button #1."
End Sub
Sub cmdButton2_OnClick()
MsgBox "You have clicked on button #2."
MyForm.cmdButton1.Click
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
This Web page consists of two buttons. If you click the first button, a message box pops up, telling you that you have clicked the first button. If you click the second button, a message box comes up, similarly saying you clicked the second button. But then, the code for the second command button executes the Click method of the first command button. That action simulates a button click of the first button. As a result, the first command button's click event is called, resulting in a second message box saying that you've clicked on the first button. In reality, you've only clicked the second button, but the code causes the click event of the first button to execute as well.
Before learning about the text control, you should be aware of new additional properties of the button control. You can use the Form property to figure out which form the button belongs to in your code. You can also use the Enabled property to enable and disable a button. This can be accomplished by setting Enabled to either True or False. There are additional methods and events available with the command button, and these will be discussed as additional controls are introduced. At the end of Day 9, you will see a table that shows each property, method, and event for all of the intrinsic HTML controls.
Another versatile and useful control in your HTML control toolkit is the text control. This control displays a simple region on the Web page into which the user can enter alphanumeric data such as numbers, strings, and so on. It's as easy to use a text control as it is the button control. The text control is another of the suite of HTML input controls and is commonly defined as follows:
<INPUT TYPE="TEXT" NAME="txtCost" SIZE="10">
Here, the TYPE attribute is set to "TEXT" rather than "BUTTON". By the way, "TEXT" is the default value of the attribute, so if you do not specify any TYPE with an input definition, you'll get a text control. Notice that you must also set the familiar NAME attribute. The typical convention when creating a text control is to prefix the name with txt. The rules for naming controls are the same no matter what the type.
The SIZE attribute is an optional attribute that enables you to specify the width of the text control in approximate characters. Due to differences in font representation, the resulting size of your text box will probably not be exactly that many characters wide on the page. If you omit the size, the browser determines a default size. Typically, you set the size equal to the maximum number of characters that you want the user to enter into the control. In a moment, you will see an example of how you can restrict the user to entering a specific number of characters as well.
The VALUE attribute is not included in the preceding example, but you can use it to assign initial text to the text box. If you want the text box to be filled with data when the Web page is loaded, you can specify it in the definition. Whatever you set for the VALUE attribute will appear in the text control when the page is loaded into the browser. It's easy to include a text control on a Web page. Listing 8.6 shows the code for a Web page that includes the text control.
Listing 8.6. Placing a simple text control on a Web page.
<HTML>
<HEAD>
<TITLE>The Text Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Text Control</EM></h1>
<HR>
<P>This Web page demonstrates how to place a simple text control on a
Web page. Later, this Web page will be expanded to retrieve the value
of the text control that the user has entered.
<P>
<INPUT TYPE="TEXT" NAME="txtData">
<INPUT TYPE="BUTTON" NAME="cmdBegin" VALUE="Get Text Control Data"
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
-->
</SCRIPT>
</BODY></HTML>
This simple text control specified the NAME but not the SIZE or VALUE. You accepted the default size and didn't supply an initial value. Figure 8.6 shows the page, which is named simptxt.htm on the CD-ROM that accompanies the book.
Figure 8.6 : A simple Web page with a text control.
Most often, all you need to be able to do is store and retrieve text from a text box, which you can accomplish using a special property of the text control called the VALUE property. If you set the VALUE property equal to a literal string or variable that stores a literal, that string or value will be stored in the text control. Similarly, if you read the VALUE property from a text control, you will get the data that is stored in that text control. Figure 8.7 shows a simple example of this.
Figure 8.7 : A Web page that gets the user's age through a text control.
Here, the user is prompted to enter his or her age. The Web page loads the text control with a default value of 25. If the user clicks the button labeled Get Age, he will see a message box that echoes the age. Listing 8.7 shows the code for this Web page, which is named impltxt.htm on the CD-ROM.
Listing 8.7. Getting a user's age from a text control and reporting it through a button using an explicit event procedure.
<HTML>
<HEAD>
<TITLE>The Text Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Text Control</EM></h1>
<HR>
<P>This Web page demonstrates how to place a simple text control on a
Web page. When you click on the button, the value in the text control
will be retrieved using the Value property and printed in a message box.
<P>
<INPUT TYPE="TEXT" NAME="txtData">
<INPUT TYPE="BUTTON" NAME="cmdBegin" VALUE="Get Text Control Data">
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdBegin_OnClick()
MsgBox "You are " & txtData.Value & " years old."
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
Notice here how the text control's text is retrieved in the subroutine that responds to the button click. The data of the text control is read by accessing the VALUE property of the control. If you wanted to place the control within a form, you would first need to refer to the form. You could refer to the form and text box on it with the name document.MyForm.txtData.Value. This is obviously a fairly long and somewhat intimidating name! Fortunately, VBScript provides a shorthand method to reference the same control. To do this, you simply declare a variable, in this case called form, and set that variable using the Set keyword so that the variable refers to the form defined in the body of your HTML document. That form is referenced using the statement
Set form = document.FormName
where FormName is the name of the form you created in your HTML document using the FORM tag and NAME attribute for that form. Notice the use of the object named document. The document object represents the entire HTML document. A form is a specific part of the entire HTML document, and that's why the form is referred to as one of the properties of the document object. To refer to the form, you must first refer to the document and then to the document's form as shown. Once you've entered this command, you can then reference any control on the form using the following syntax:
form.control.property
With the cmdResult_OnClick subroutine, you must use
form.txtData.Value
to refer to the text of the text control you want if you place the controls within a form. You might be a bit unsure about documents and forms right now, but they will be covered in more detail on subsequent days. Day 14, "Working with Documents and User Interface Functions," includes a further look at the document object, and Day 19 looks at using a form to submit data to the server. For now, just remember that you access a control through a form, and you access a form through a document. If you use the structure outlined previously, you will make the necessary connections in your code.
As you can see from Listing 8.7, the input controls we used were defined without enclosing them in a form. If you simply declare a text box control:
<INPUT TYPE="TEXT" NAME="txtSample">
you can then refer to the name txtSample anywhere in your script. For example, you could assign a new value to it:
txtSample.Value = "37"
You don't need to reference the document because it is automatically recognized as part of the document. Similarly, because the control is not a part of any form, you don't need to make a form reference. This is an easier way to define and reference controls. Keep in mind, however, the restrictions of defining intrinsic controls outside of forms that were discussed earlier. The contents of Controls defined outside of forms will not be submitted to CGI Server Scripts even if you provide a submit button.
The MAXLENGTH property is another attribute of the text control worth mentioning. This enables you to restrict the number of characters a user can enter in a text control. Why is this useful? Suppose you wanted the user to enter a five digit zip code; obviously, you wouldn't want him to enter six or seven characters! Suppose he had to enter his age. He wouldn't need more than three characters. Why allow him to type more than three? If he does so by accident, he will be pleasantly surprised that your Web page is smart enough to stop him from entering more than three characters. Simply set the MAXLENGTH attribute equal to the maximum number of characters you want to allow the user to enter.
To see how this works, consider the revised Web page that asks for the user's age. This time, the MAXLENGTH property is set to three so the user is restricted to that number of allowable characters. Listing 8.8 shows this "more text example," which is found on the CD-ROM under the name moretxt.htm.
Listing 8.8. Getting a user's age with a text control and reporting it through a button, restricting the age to three characters.
<HTML>
<HEAD>
<TITLE>The Text Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>More on the Text Control</EM></h1>
<HR>
<P>This Web page demonstrates how to place a simple text control on a
Web page. Click on the button to retrieve the value of the text
control that the user has entered.
<P>
<FORM NAME="MyForm">
Enter your age: <INPUT TYPE="TEXT" NAME="txtData" SIZE="5" MAXLENGTH="3">
<INPUT TYPE="BUTTON" NAME="cmdResult" VALUE="Get Age">
</FORM>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdResult_OnClick()
Dim form
Set form = document.MyForm
MsgBox "You are " & form.txtData.Value & " years old!"
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
Keep in mind that the MAXLENGTH attribute specifies how many characters you can enter. SIZE determines how big the control is in approximate character units. If you make MAXLENGTH larger than SIZE, you will only be able to enter text until you hit the right edge of the text control. For instance, if you make SIZE equal to 3 and MAXLENGTH equal to 30, you will run out of space before you have entered 30 characters. You might be able to squeeze in more than three characters, but your space restrictions will prevent you from entering them all.
Like the button control, the text control also has useful events and methods you can use. Two equally useful events are the OnFocus and OnBlur events. The OnFocus event occurs whenever a control, in this case the text control, receives focus. A control can receive focus in one of two ways-the user can use the mouse or press the Tab key to place focus on the control, or the developer can use the Focus method to put the control into focus using code (this was the case with the Click event using the Click method). When a control receives focus, a gray box typically silhouettes the control.
Focus is a term used to indicate a control on a Web page that has special recognition as being the control the user is interested in. When a control has focus, certain keys cause the control to react in some way. For example, pressing the Enter key when a button has focus simulates clicking on the button.
Consider the Web page named onfocus.htm, shown in Figure 8.8.
Figure 8.8 : A Web page that uses the OnFocus event and Focus method of text control.
The code for this Web page is shown in Listing 8.9.
Listing 8.9. Using the Focus method and OnFocus event of a text control.
<HTML>
<HEAD>
<TITLE>The OnFocus and OnBlur Events</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Button Control</EM></h1>
<HR>
<CENTER><H2>Using the OnFocus and OnBlur events</H2></CENTER>
<P>This Web page demonstrates how the OnFocus and OnBlur events are
used to control code flow in a program.
<FORM NAME="MyForm">
<P>Textbox #1 - <INPUT TYPE="TEXT" SIZE="50" NAME="txtControl1" >
<P>Textbox #2 - <INPUT TYPE="TEXT" SIZE="50" NAME="txtControl2" >
</FORM>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and _
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub txtControl1_OnFocus()
Dim form
Set form = document.MyForm
form.txtControl1.Value = "This control has focus!"
End Sub
Sub txtControl1_OnBlur()
Dim form
Set form = document.MyForm
form.txtControl1.Value = "This control has lost focus!"
End Sub
Sub txtControl2_OnFocus()
Dim form
Set form = document.MyForm
form.txtControl2.Value = "This control has focus!"
End Sub
Sub txtControl2_OnBlur()
Dim form
Set form = document.MyForm
form.txtControl2.Value = "This control has lost focus!"
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
This Web page consists of two text controls. If you click one of the text controls, giving it focus, it indicates it has focus. The other button will not have focus. If you click the second button, the first will indicate that it has lost focus while the second has gained the focus. You can see by clicking both controls how the focus switches from one control to the other. The OnFocus and OnBlur events are being fired, respectively, as you alternate between the text controls.
The button control discussed in the previous section also supports the OnClick and OnFocus events, as well as the Focus method. As for the text control, in addition to supporting the OnFocus and OnBlur events, it also has events such as OnChange, which is executed whenever the contents of a text control change. The OnSel event is another event available to the programmer that gets executed whenever the user selects text within the control. The text control has more useful methods as well. You can use the Focus and Blur methods, which cause their respective events to execute, and you can also use the Select method, which automatically selects the text in the control and calls the OnSelect event. Finally, the text control has several useful properties you can use. The defaultValue property can be used to set a text control to some initial value when the Web page loads into the browser. The Enabled property can also be used to enable or disable the user from changing the contents of a text control. These capabilities were not present in early beta versions of Internet Explorer 3.0, but added in subsequent versions.
The text control is powerful, necessary, and useful to many Web pages. What if you don't want to restrict the users to entering just one line of text? What if you want them to be able to use a whole area of text, such as when they want to provide a description that spans several lines? In that case, you need another special control called a textarea control.
The textarea control is a special type of control for those who want to extend the power of a regular text control. Textarea controls, unlike regular text controls, can contain many lines of text. This is very useful when you want the user to enter more than one line of information. Depending on the browser, the user can use scroll bars to move across and down the textarea. Some browsers also wrap text when it reaches the end of a specific line.
The textarea control differs from regular text controls in that the textarea control has both a starting and ending tag. In the textarea start tag, you should specify at least three attributes: the NAME, ROWS, and COLS attributes. As with the other controls, the NAME attribute gives the control a name. The naming convention recommended for the textarea control is the prefix txa before the name of the control. If you wanted to call your control MailNote, you could use the convention txaMailNote to name the control.
ROWS specifies the height of the textarea, where the units are the number of rows of text displayed between the top and bottom of the area. COLS specifies the width of the textarea. Again, the units are characters. Keep in mind one important thing about the ROWS and COLS properties: They simply specify the size of the control, not how much text you can place within it. The behavior of the textarea control depends, in part, on the browser. Suppose you specify that ROWS equal 10, which means the textarea control will be ten lines high. Similarly, suppose you set the COLS attribute to 50, making the textarea control space 50 characters wide. All the browsers will give you the space on the screen you specify, but some browsers will not let you enter text beyond the right edge of a textarea control. Other browsers enable you to enter text beyond the right border by providing scroll bars to let you adjust the window to cover the area of text. Still other browsers automatically wrap the text to the next row when you've reached the right edge of the control on a given row. The formatting depends on the browser. Microsoft Internet Explorer provides scroll bars for both columns and rows so you can scroll through the text you have entered.
The following line is an example of the syntax of a textarea control:
<TEXTAREA NAME="txaMemo" ROWS="20" COLS="50"></TEXTAREA>
With the textarea control, you cannot use the VALUE property to initially place text in the textarea as the Web page loads in the browser. Instead, you must enclose in textarea tags any initial text that you want assigned when the page loads:
<TEXTAREA NAME="txaMemo" ROWS="20" COLS="50">This is my initial data!</TEXTAREA>
However, the program can use the VALUE property, as it did with the text control, to supply text to the textarea control or retrieve data from it.
Consider the sample Web page shown in Figure 8.9. This simple textarea Web page, called simptxa.htm, is on the CD-ROM that accompanies the book.
Figure 8.9 : A Web page that uses a textarea control.
As you can see, Internet Explorer provides scroll bars on the sides of the textarea so that you can navigate beyond the borders of the control. Listing 8.10 shows the code.
Listing 8.10. A simple Web page that uses a textarea control.
<HTML>
<HEAD>
<TITLE>The TextArea Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The TextArea Control</EM></h1>
<HR>
<P>This Web page demonstrates how to place a text area control
on a Web page.
<P>
<TEXTAREA NAME="txtData" ROWS="10" COLS="60"></TEXTAREA>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
-->
</BODY></HTML>
The browser usually chooses a fixed font, such as Courier, although it depends on the browser. You can also change the font using the necessary HTML tags, if you want. Retrieving text to and from the textarea control is really quite simple. You simply use the VALUE property of the control as you did with the text control. Common uses of the textarea control are cases where the user must enter more than one line of data, such as an address, list of information, a memo or mail to another user, and so on.
Listing 8.11 shows a simple example of retrieving text from a textarea control much like the example using the text control in Listing 8.7. This implicit textarea sample Web page, called impltxa.htm, is on the CD-ROM that accompanies this book.
Listing 8.11. A Web page that uses the textarea control to retrieve text.
<HTML>
<HEAD>
<TITLE>The TextArea Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The TextArea Control</EM></h1>
<HR>
<P>This Web page demonstrates how to place a simple text control on a Web
page. Click on the button to retrieve the value of the text control that
the user has entered.
<P>
<TEXTAREA NAME="txtMemo" ROWS="10" COLS="60"></TEXTAREA>
<INPUT TYPE="BUTTON" NAME="cmdShow" VALUE="Show the Text">
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdShow_OnClick()
MsgBox "The text you have entered is: " & txtMemo.Value
End Sub
-->
</SCRIPT>
</BODY></HTML>
This Web page shows the text of the textarea control in a message box when the user clicks the Show the Text button. You can use this control together with the other two you have seen to help build a useful and effective Web page.
Note |
You can also assign individual lines of text to the textarea programmatically. The sample program PPalErr5.htm, which you will see on Day 17, "Exterminating Bugs from Your Script," uses this advanced technique to generate a debug window on a Web page. |
The textarea control, like the text control, also supports the defaultValue and Enabled properties; the Focus, Blur, and Select methods; and the onFocus, onBlur, onChange, and onSel events. A complete summary of each intrinsic control and the properties, events, and methods it supports is summarized in Day 9.
Now that you've seen each control and simple examples of how they work, consider them together in a simple example. Use the familiar example of entering an order-in this case, for concert tickets. This example uses the text control, the textarea control, and the button all together to retrieve data from the user. Figure 8.10 shows the Web page called tickets.htm.
Figure 8.10 : Web page to order concert tickets.
Listing 8.12 shows the source code for this Web page.
Listing 8.12. Source code for the Ticket Booth Web page.
<HTML>
<HEAD>
<TITLE>The TextArea Control</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://www.mcp.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>The Ticket Booth</EM></h1>
<HR>
<P>Come see "Jamboree!" in concert July 4, 1996 at the Kick Stand in
Grand Rapids, Michigan. Tickets are $20 per person. Give us your name,
address and credit card number and we'll mail you your tickets.
<P>It's a concert you won't want to miss! Come hear "Jamboree!" on
their opening tour, <EM>Driving through the Midnight Sun</EM>.
<P>To order tickets today, please enter the following information and
click on the order button.
<HR>
<FORM NAME="MyForm">
<PRE>Name <INPUT TYPE="TEXT" NAME="txtName" SIZE="20"></PRE>
<PRE>Address <TEXTAREA NAME="txaAddress" COLS="40"
ROWS="5"></TEXTAREA></PRE>
<P>
<PRE>Ticket Qty. <INPUT TYPE="TEXT" NAME="txtTickets"
SIZE="5" MAXLENGTH="5"> tickets</PRE>
<PRE>Credit Card Number <INPUT TYPE="TEXT" NAME="txtCard"
SIZE="19" MAXLENGTH="19"></PRE>
<P>
<INPUT TYPE="BUTTON" NAME="cmdOrder" VALUE="Mail My Tickets!">
</FORM>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Days</em><br>
by <A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A REF="../shared/info/tim.htm">Tim Koets</A><br>
<br>
Return to <A href=Back08.htm>content overview</A><br>
Copyright 1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdOrder_OnClick()
Dim form
Dim Name
Dim Address
Dim CreditCard
Dim Quantity
Dim Cost
Set form = document.MyForm
If form.txtName.Value = "" Then
MsgBox "You need to enter your name!"
ElseIf form.txaAddress.Value = "" Then
MsgBox "You need to enter your address!"
ElseIf form.txtCard.Value = "" Then
MsgBox "You need to enter your credit card number."
ElseIf form.txtTickets.Value = "" Then
MsgBox "You should order at least one ticket!"
Else
Name = form.txtName.Value
Address = form.txaAddress.Value
Quantity = form.txtTickets.Value
CreditCard = form.txtCard.Value
Cost = Quantity * 20
MsgBox "An order for " & Quantity & " tickets has been placedÂ
for " & Name & " for a total cost of " & Cost & " dollars.Â
Enjoy the show! Your tickets will arrive in 5 to 10 days."
End If
End Sub
-->
</SCRIPT>
</BODY></HTML>
Notice that the textarea is used to enter the user's address, which could eventually be printed on a mailing label. You could submit the user's name to a server, along with the credit card number (in a secure fashion, of course) and the total cost. The credit card number is restricted to 19 spaces, which includes four sets of four numbers plus three spaces. The button control triggers a script.
The script also has some very simple validation code to make sure the user enters all the required information. This validation code could be much more sophisticated, but it serves its purpose here. You could add even more validation code and then trigger a special form method called Submit that would send the data to a CGI script program on the server, which would actually process the data. The message box that appears to the user could, in reality, be a Web page that the server sends back to the user with order information. To keep the code clear and simple, the actual submittal code was not included with this example. You will learn more about that process on Day 19.
Today's lesson presents you with three intrinsic HTML form controls: the button control, the text control, and the textarea control. Each of these controls, traditionally called form input controls, are defined by the HTML standard and usually used in conjunction with an HTML form. You have learned what an HTML form is, as well as how it is used, which helped better define what a "form control" is. Because these controls are intrinsic to HTML and browsers, they do no not need to be "registered" or "loaded" into the program as OLE and ActiveX controls and components do. You'll learn more about those on Day 10.
You have learned how to create button controls and place them on a Web page. Furthermore, you have learned how to hook up code in VBScript Web pages by using event procedures. Today's lesson also presented the text and textarea controls, and you learned how define and place them on a Web page. The user can use the VALUE property of these controls in VBScript to send and retrieve information to a text control.
For more information on HTML, refer to one of the resources mentioned today and in Appendix B, "Information Resources." Tomorrow, you will learn about some of the other popular HTML form controls. By then, you will have a good, working knowledge of all these controls. Once you've also learned ActiveX controls, you will be able to present very powerful Web pages to your users.
What is the difference between ActiveX controls and HTML controls? | |
The main difference between the two is that HTML controls are defined as part of the HTML language standard. ActiveX controls are extras that you can add to a Web page but are not part of HTML. The ActiveX control standard that was created by Microsoft can give the programmer more powerful and capable controls than what HTML controls provide. HTML controls provide many of the fundamental controls that you use every day such as the text control and the button. ActiveX controls add extra pizzazz to your Web pages by providing you services that take you beyond the conventional HTML controls such as fancy labels that can be displayed at an angle. | |
Why can't I have better control over the placement and size of the controls as I can in Visual Basic? | |
The whole philosophy behind HTML is that it enables the browser to do the formatting for you. Any control based on HTML adheres strongly to that principle. For example, you cannot adjust the size of a button; the browser does it for you. You can set the length of the text control, and the textarea properties enable you to specify the dimensions of the control. However, you cannot specify X and Y coordinates for placing controls on a page. You must adhere to the HTML standards. | |
Are there any limits to the amount of text the user can enter in a textarea control? | |
There is a limit, but it's pretty large. The current limit is approximately 65,000 characters. The user is not likely to enter that much information, however. If he wants to send information of that magnitude, a better approach might be to provide him with the capability to FTP a file directly to the server rather than work through a textarea control on a Web page. |
Create a Web page that uses each of the controls presented today. Make the goal of your Web page to get information from the user from various text fields. Do you find that text and textarea controls are adequate to get what you want from the user? What are the advantages of the text and textarea controls? What are their disadvantages?
Note |
Refer to Appendix C, "Answers to Quiz Questions," for the answers to these questions. |