Revising the Calculation Script with Live Data

Both times before, when you output data from a database, you did so directly into HTML. You output the Traveler's Journal entry into the main page in index.asp, and you output the tourName/tourID pairing into a form menu element. In this step, you don't need to output the adult and child prices anywhere that the user can see them. In fact, they need to be output into the calculation itself. As you'll see, though, you output internally to a script the same way you do to XHTML.

Before you do that, though, let's add one more simple piece of functionality: Let's display the name of the tour that users just calculated, in case they forgot or chose the wrong one.

  1. In tourprice_processor.asp, switch to design view. After the word tour in the first sentence, add the following text:, XX,. Select XX, and apply bolding to it, using the Property inspector.

    Once again, the Xs are used as placeholder text. The advantage to using this text is that you can format it the way you want your dynamic content to appear, in this case, bold.


  2. Select the XX text, and switch to the Application tab of the Insert panel. Click the Dynamic Text button.

    The Application tab is used to insert common dynamic elements, such as dynamic text.


  3. In the Dynamic Text dialog, expand Recordset (rs_tourprices), and click tourName. Click OK.


    What you've just done is equivalent to dragging a binding from the Bindings panel onto the page. You should also be able to understand the code Dreamweaver outputs in the code window.

    In ASP, the following is added where XX appeared before: <%=(rs_tourprices. Fields.Item("tourName").Value)%>. Remember, <%= (as opposed to <%) means <% Response.Write, so this is an output script. As you can probably guess from the rest, it is outputting the value of the tourName item, which is a field, in the recordset rs_tourprices.

    In ColdFusion, the following code appears: <cfoutput>#rs_tourprices.tourName#</cfoutput>. Again, the <cfoutput> tag is equivalent to ASP's Response.Write, and what is being output is the tourName value stored in rs_tourPrices.

    Reading and understanding the code is important, because in the next step, you'll have to do some hand-coding.

  4. In code view, find the calculation script. Delete the line that sets the value of the basePrice variable to the form variable tourName.

    The line in ASP that you are deleting is basePrice=Request.Form("tourName"), and it should be around line 38 (plus or minus) in code view.

    The line in ColdFusion that you are deleting is <cfset basePrice = form.tourName>, and it should be around line 12 (plus or minus) in code view.

    This line is no longer necessary, because you are not obtaining the base price from the form anymore, but rather from the database. In addition, there are two prices that you now need to use: the one for children and the one for adults.


  5. Add two new lines of code that set the value of two new variables: basePriceAdult and basePriceChild.

    In ASP:


    In ColdFusion:

    <cfset basePriceAdult = #rs_tourprices.basePriceAdult#>
    <cfset basePriceChild = #rs_tourprices.basePriceChild#>

    Both of these scripts create two new variables and set the values to the appropriate adult and child prices as retrieved from the database.


  6. ASP users only: Remove basePrice from the Dim line and add basePriceAdult and basePriceChild to the list.

    Since ASP requires explicit declaration of all variables, you have to remember to update that declaration whenever you change your variables.

  7. Update the basePrice variables in the calculation line itself with the appropriate new variables.

    In ASP:

    tourPrice = (numAdult * basePriceAdult) + (numChild * basePriceChild)

    In ColdFusion:

    <cfset tourPrice = (numAdult * basePriceAdult) + (numChild * basePriceChild)>

    Now the calculation reflects the values that you have retrieved from the database.


  8. Save and upload tourprice_processor.asp. In the Site panel, select tourprice.asp and press F12 to test the application.

    The application works as it should. The application outputs the correct calculation based on the information the user entered. Best of all, maintaining this application is as easy as maintaining the database. If a price goes up and the new value is added to the database, the calculator will reflect that immediately. If you add or remove an entire tour, that will be reflected in the application as well.