Schongar P.VBScript unleashed.1997
.pdflExtPrice = pdPrice * pzQtyText.Text
pzQtyHidden.value = pzQtyText.Text
GetPrice = FormatCurrency(lExtPrice)
end function
Function FormatCurrency(ptInputValue)
Dim ltValue
ltValue = ptInputValue
if instr(ltValue,".") = 0 then
ltValue = ltValue & ".00"
elseif instr(ltValue,".") = Len(ltValue) - 1 then
ltValue = ltValue & "0"
end if
ltValue = "$" & ltValue
FormatCurrency = ltValue
end function
Sub UpdateTotals()
dim lTmp
lTmp = CInt(document.order.LincolnQty.Value )
lTmp = lTmp + CInt(document.order.RooseveltQty.Value)
lTmp = lTmp + CInt(document.order.ReaganQty.Value)
lTmp = lTmp * 4.95
window.parent.frames(1).lblShipTot.Caption = cstr(FormatCurrency (lTmp))
Dim lTmp2
lTmp2 = CDbl(document.order.lblLincolnExt.Caption)
lTmp2 = lTmp2 + CDbl(document.order.lblRooseveltExt.Caption)
lTmp2 = lTmp2 + CDbl(document.order.lblReaganExt.Caption)
window.parent.frames(1).lblCostTot.Caption = cstr(FormatCurrency (lTmp2))
window.parent.frames(1).lblGrandTot.Caption = cstr(FormatCurrency (lTmp + ltmp2))
end sub
Sub txtLincolnQty_Change()
Dim lExtPrice
lExtPrice = GetPrice(document.order.txtLincolnQty, document.order. LincolnQty, 24.95)
if lExtPrice = -1 then exit sub
Document.ORDER.lblLincolnExt.caption = cstr(lExtPrice)
Call UpdateTotals()
end sub
Sub txtRooseveltQty_Change()
Dim lExtPrice
lExtPrice = GetPrice(document.order.txtRooseveltQty, document.order. RooseveltQty, 22.95)
if lExtPrice = -1 then exit sub
Document.ORDER.lblRooseveltExt.caption = cstr(lExtPrice)
Call UpdateTotals()
end sub
Sub txtReaganQty_Change()
Dim lExtPrice
lExtPrice = GetPrice(document.order.txtReaganQty, document.order. ReaganQty, 14.95)
if lExtPrice = -1 then exit sub
Document.ORDER.lblReaganExt.caption = cstr(lExtPrice)
Call UpdateTotals()
end sub
-->
</SCRIPT>
<OBJECT ID="txtLincolnQty" WIDTH=71 HEIGHT=24
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002f3">
<PARAM NAME="VariousPropertyBits" VALUE="746604571">
<PARAM NAME="Size" VALUE="1873;635">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<INPUT TYPE=hidden VALUE="0" SIZE=5 NAME="LincolnQty" maxlength=256>
</td><td width=25%>
<OBJECT ID="lblLincolnExt" WIDTH=83 HEIGHT=16
CLASSID="CLSID:99B42120-6EC7-11CF-A6C7-00AA00A47DD2">
<PARAM NAME="_ExtentX" VALUE="2196">
<PARAM NAME="_ExtentY" VALUE="423">
<PARAM NAME="Caption" VALUE="$0.00">
<PARAM NAME="Angle" VALUE="0">
<PARAM NAME="Alignment" VALUE="0">
<PARAM NAME="Mode" VALUE="1">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="ForeColor" VALUE="#000000">
<PARAM NAME="BackColor" VALUE="#C0C0C0">
<PARAM NAME="FontName" VALUE="Arial">
<PARAM NAME="FontSize" VALUE="12">
<PARAM NAME="FontItalic" VALUE="0">
<PARAM NAME="FontBold" VALUE="0">
<PARAM NAME="FontUnderline" VALUE="0">
<PARAM NAME="FontStrikeout" VALUE="0">
<PARAM NAME="TopPoints" VALUE="0">
<PARAM NAME="BotPoints" VALUE="0">
</OBJECT>
</td></tr>
<tr><td width=25%>Roosevelt</td><td width=25%>$22.95/$4.95</td><td width=25%>
<OBJECT ID="txtRooseveltQty" WIDTH=71 HEIGHT=24
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002f3">
<PARAM NAME="VariousPropertyBits" VALUE="746604571">
<PARAM NAME="Size" VALUE="1873;635">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<INPUT TYPE=hidden VALUE="0" SIZE=5 NAME="RooseveltQty" maxlength=256>
</td><td width=25%>
<OBJECT ID="lblRooseveltExt" WIDTH=83 HEIGHT=16
CLASSID="CLSID:99B42120-6EC7-11CF-A6C7-00AA00A47DD2">
<PARAM NAME="_ExtentX" VALUE="2196">
<PARAM NAME="_ExtentY" VALUE="423">
<PARAM NAME="Caption" VALUE="$0.00">
<PARAM NAME="Angle" VALUE="0">
<PARAM NAME="Alignment" VALUE="0">
<PARAM NAME="Mode" VALUE="1">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="ForeColor" VALUE="#000000">
<PARAM NAME="BackColor" VALUE="#C0C0C0">
<PARAM NAME="FontName" VALUE="Arial">
<PARAM NAME="FontSize" VALUE="12">
<PARAM NAME="FontItalic" VALUE="0">
<PARAM NAME="FontBold" VALUE="0">
<PARAM NAME="FontUnderline" VALUE="0">
<PARAM NAME="FontStrikeout" VALUE="0">
<PARAM NAME="TopPoints" VALUE="0">
<PARAM NAME="BotPoints" VALUE="0">
</OBJECT>
</td></tr>
<tr><td width=25%>Reagan</td><td width=25%>$14.95/$4.95</td><td width=25%>
<OBJECT ID="txtReaganQty" WIDTH=71 HEIGHT=24
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002f3">
<PARAM NAME="VariousPropertyBits" VALUE="746604571">
<PARAM NAME="Size" VALUE="1873;635">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<INPUT TYPE=hidden VALUE="0" SIZE=5 NAME="ReaganQty" maxlength=256>
</td><td width=25%>
<OBJECT ID="lblReaganExt" WIDTH=83 HEIGHT=16
CLASSID="CLSID:99B42120-6EC7-11CF-A6C7-00AA00A47DD2">
<PARAM NAME="_ExtentX" VALUE="2196">
<PARAM NAME="_ExtentY" VALUE="423">
<PARAM NAME="Caption" VALUE="$0.00">
<PARAM NAME="Angle" VALUE="0">
<PARAM NAME="Alignment" VALUE="0">
<PARAM NAME="Mode" VALUE="1">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="FillStyle" VALUE="0">
<PARAM NAME="ForeColor" VALUE="#000000">
<PARAM NAME="BackColor" VALUE="#C0C0C0">
<PARAM NAME="FontName" VALUE="Arial">
<PARAM NAME="FontSize" VALUE="12">
<PARAM NAME="FontItalic" VALUE="0">
<PARAM NAME="FontBold" VALUE="0">
<PARAM NAME="FontUnderline" VALUE="0">
<PARAM NAME="FontStrikeout" VALUE="0">
<PARAM NAME="TopPoints" VALUE="0">
<PARAM NAME="BotPoints" VALUE="0">
</OBJECT>
</td></tr>
</table>
</center></div>
</FORM>
<!-- FINAL SCRIPT SECTION -->
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdSubmit_Click()
dim lTmp
if ValidateLen(Document.Order.FirstName, "First Name") = 0 then exit sub
if ValidateLen(Document.Order.LastName, "Last Name") = 0 then exit sub
if ValidateLen(Document.Order.Address, "Address") = 0 then exit sub
if ValidateLen(Document.Order.City, "City") = 0 then exit sub
if ValidateLen(Document.Order.State, "State") = 0 then exit sub
if ValidateLen(Document.Order.ZIP, "ZIP Code") = 0 then exit sub
lTmp = (Document.ORDER.ZIP.value)
select case Len(lTmp)
case 5
if not isnumeric(lTmp) then
Call ErrorOnZIP()
exit sub
end if
case 10
if mid(lTmp,6,1) <> "-" then
call ErrorOnZIP()
exit sub
end if
if not IsNumeric(left(lTmp,5)) then
Call ErrorOnZIP()
exit sub
end if
if not isnumeric(right(lTmp,4)) then
Call ErrorOnZIP()
exit sub
end if
case else
Call ErrorOnZIP()
exit sub
end select
Document.Cookie = "FirstName=" & document.order.FirstName.Value
Document.Cookie = "LastName=" & Document.Order.LastName.Value
Document.Cookie = "Address=" & Document.Order.Address.Value
Document.Cookie = "City=" & Document.Order.City.Value
Document.Cookie = "State=" & Document.Order.State.Value
Document.Cookie = "ZIP=" & Document.Order.ZIP.Value
Document.Cookie = "Phone=" & Document.Order.Phone.Value
Document.Cookie = "LastOrderDate=" & CStr(Now)
call Document.ORDER.submit()
end sub
Function ValidateLen(pzField, ptMessage)
if Len(pzField.value) = 0 then
MsgBox ptMessage & " is required!"
call pzField.Focus()
ValidateLen = 0
exit function
end if
ValidateLen = 1
end function
Sub ErrorOnZIP()
MsgBox "ZIP Code invalid!"
call Document.ORDER.ZIP.Select
call Document.ORDER.ZIP.focus()
end sub
-->
</SCRIPT>
</BODY></html>
The script code begins with the window_onLoad event. The first step of the script is to load any cookie data that exists for the order entry page. The cookie data is saved when the user clicks the Submit button. The onLoad event calls the ReadVariable() function discussed in Chapter 14. This function reads a cookie variable from the cookie property
and returns the cookie's value. The cookie data then is placed into the appropriate text boxes on the form.
After the cookie data is loaded, the focus is set to the first name text box by invoking the focus() method. This is necessary because you cannot guarantee that the first name box will receive focus when the page loads. Usually, one of the ActiveX text boxes receives focus before the HTML form does.
Scrolling further down the listing, find the comment <!-- MORE SCRIPT CODE -->. This section of code is where calculations are made based on the quantities entered by the user. The first function, GetPrice(), ensures that the user entered an integer and then calculates the total price for the line on which the quantity was entered. Note the use of the IsNumeric() function. Because the Visual Basic val() function does not appear to work within VBScript, you should use IsNumeric() instead of if val(variable_name) = 0 to determine whether variable_name stores a numeric value.
Next comes the FormatCurrency() function. Because VBScript lacks the Visual Basic format$() function, you must manually format strings. The FormatCurrency() function takes a string and formats it as $#.00 would in the format$() function.
Next comes the UpdateTotals procedure. This procedure updates the ActiveX labels found in the bottom frame. The first section of code in Listing 25.3 updates the label that displays the total shipping cost. The second section updates the total cost of the portraits, not including shipping. The final line of code updates the label displaying the grand total.
Finally, the change event code is used for the three ActiveX text boxes. All three event procedures perform the same tasks. First, the GetPrice() function is called to obtain the total product cost for the current portrait. If the quantity entered was valid, the ActiveX label displaying the extended cost for the portrait (quantity multiplied by price) is updated. Finally, the UpdateTotals procedure is called to update the bottom frame.
The final section of code appears after the <!-- FINAL SCRIPT SECTION --> comment. This code is responsible for validating the data entered into the form. The validation rules follow:
●All user information fields must have values.
●The ZIP code entered must be in ##### format or #####-#### format.
WARNING
Early versions of Internet Explorer 3.0 appear to submit an HTML form whenever the user presses the Enter key, bypassing the validation code placed in the command button's Click event. If you come up with a clever way to avoid this, e-mail me at craige@pipestream. ipctech. com.
The Submit button is not an HTML form's Submit button but rather an ActiveX command button control. Clicking this button fires the cmdSubmit_click event. This event first uses a function named ValidateLen() to check the existence of data in the specified text box. If the text box is empty, a message box is displayed and the function returns a 0. The HTML form data is not submitted in this case. After all the user information boxes are validated as having data,
the ZIP code field is checked to make sure that the data is in the proper format.
After all the validation checks have passed, the script updates the cookie data. The current values entered in the text boxes are stored into the cookie. The current date/time also is stored in a cookie to store the date/time of the user's last order submission.
Finally, the form's submit() method is invoked to send the form's data to the back-end process that is servicing the order entry application.
Testing the Application
After you save or copy the code for the three listings from the CD-ROM, you can test the script. If you want to use the cookie feature, you must save the files to a Web server's content directory because cookies depend on a Web server for their operation.
Load the file 25-1.htm. It should appear as in Figure 25.2. Enter information into all the user information boxes at the top of the page. Try entering some invalid ZIP codes and clicking the Submit button. If you don't receive a message box informing you that the ZIP code is invalid, check the code in the cmdSubmit_click event. Also, if you can leave a user information field blank without receiving a message box, check to make sure that the field is included in the calls to
ValidateLen().
Next, try entering some quantities into the table at the bottom of the form. You should see the appropriate label captions change. Try entering a number other than an integer. A message box should appear asking you to enter an integer. If not, check the code in the GetPrice() function.
After testing the other portions and successfully submitting the form, reload the page. The user information text boxes should contain the data previously entered. If not, first check the cmdSubmit_click event to make sure that the cookie property is being set for any missing information. If everything looks fine there, check the window_onLoad event to make sure that the data is being read for the missing field. If all fields are missing, check the code in ReadVariable() and make sure that you're using the Web server to provide the page, instead of using Internet Explorer's File | Open menu to load the operating system.
Review
The example in this chapter provides the basis for a fully operational order entry Web application. The only piece you must add is the back-end application that processes the submitted data. This is another subject for another book. Web Programming with Visual Basic, published by Sams.net, is an excellent source for learning how to program back-end Web server applications using Visual Basic.
