Creating the Conditional Region

The idea of a region of HTML that can be shown or hidden may sound fancy, but in fact you can do it using skills you have learned in this chapter. All you do is embed standard HTML inside an if statement. If that evaluates to true, then the HTML is displayed. If it evaluates to false, then it skips the HTML and continues.

  1. Open tourprice.asp. Position the cursor before the opening <form> tag, and press Enter/Return a few times to make room for some new code.

    You should find the opening <form> tag around line 38. By inserting the conditional region here, you cause the error message to appear in a prominent location when the page reloads.

    graphics/07inf05.gif

  2. As before, start with the outer shell, by writing the if statement.

    In ASP:

    <%
    If Request.QueryString("error") = "notnumeric" Then
    
    End If
    %>
    

    In ColdFusion:

    <cfif url.error is "notnumeric">
    
    </cfif>
    

    The if statements here test to determine whether there is a querystring (or URL) variable called error, and if so, whether its value is set to "notnumeric." When the page first loads, there is no querystring or URL variable named error, so this if statement would evaluate to false. As you have seen, however, if the page has been redirected back to tourprice.asp from the form validation script on tourprice_processor.asp, the querystring exists with that value.

  3. Nested between the opening and closing if lines, insert the code that tells ASP/ColdFusion to output the desired HTML.

    In ASP:

    [View full width]
    Response.Write("<p>*** Error! One or more fields was left blank or contained a graphics/ccc.gifnon-numeric character.</p>")

    In ColdFusion:

    [View full width]
    <cfoutput><p>*** Error! One or more fields was left blank or contained a non-numeric graphics/ccc.gifcharacter.</p></cfoutput>

    Both Response.Write and <cfoutput> can be used to output static or dynamic code. We've used them to output dynamic code thus far, but there is no reason why you can't put static code in there as well, or any combination of static and dynamic code.

    graphics/07inf06.gif

  4. ColdFusion users only: Wrap the entire <cfif> script in another <cfif> script, so that the original <cfif> script only runs if the URL variable error actually exists.

    [View full width]
    <cfif isDefined("url.error")> <cfif url.error is "notnumeric"> <cfoutput><p>*** Error! One or more fields was left blank or contained a ® graphics/ccc.gifnon-numeric character.</p></cfoutput> </cfif> </cfif>

    The function isDefined works much like isNumeric, except that rather than testing whether the parameter is a number, it tests to see whether the parameter exists.

    This extra code is necessary in ColdFusion, because ColdFusion assumes that if you are testing a variable (<cfif url.error = "notnumeric">), then that variable exists. If it does not exist, and you attempt to test it, ColdFusion displays an error message. The error URL variable exists only when the page loads as a result of a redirection from the form validation on tourprice_processor.cfm. Thus, when the page first loads, an ugly error message dominates the page. We solve the problem by testing to ensure that url.error is defined. If it is not, then ColdFusion ignores the directions that test whether error's value is set to notnumeric. If url.error is defined, ColdFusion continues with the test, as before.

    ASP differs from ColdFusion in this instance, in that if querystring.error is undefined, then it knows error can't be equal to notnumeric and it proceeds as expected.

    graphics/07inf07.gif

  5. Save and upload tourprice.asp. Test it in a browser.

    The error message now appears when you fail to enter numbers in both fields.

    There's only one problem left: The error message isn't very conspicuous, is it?

    graphics/07fig14.gif