Adobe COLDFUSION 9 Manual page 172

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Building Blocks of ColdFusion Applications
Code
<cfform method="POST" action="#CGI.script_name#">
<p>Enter your Name:&nbsp;
<input name="name" type="text" hspace="30"
maxlength="30">
<input type="Submit" name="submit" value="OK">
</cfform>
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your
else WriteOutput("Hello " & name &"!"); return "";
}
if (IsDefined("Form.submit"))
HelloFriend(Form.name);
</cfscript>
Providing status information
In some cases, such as those where the function cannot provide a corrective action, the function cannot, or should not,
handle the error directly. In these cases, your function can return information to the calling page. The calling page
must handle the error information and act appropriately.
Consider the following mechanisms for providing status information:
• Use the return value to indicate the function status only. The return value can be a Boolean success/failure
indicator. The return value can also be a status code, for example where 1 indicates success, and various failure types
are assigned known numbers. With this method, the function must set a variable in the caller to the value of a
successful result.
• Set a status variable that is available to the caller (not the return variable) to indicate success or failure and any
information about the failure. With this method, the function can return the result directly to the caller. In this
method, the function uses only the return value and structure arguments to pass the status back to the caller.
Each of these methods can have variants, and each has advantages and disadvantages. The technique that you use
depends on the type of function, the application in which you use it, and your coding style.
The following example, which modifies the function used in
version of the status variable method. It provides two forms of error information:
• It returns -1, instead of an interest value, if it encounters an error. This value can serve as an error indicator because
you never pay negative interest on a loan.
• It also writes an error message to a structure that contains an error description variable. Because the message is in
a structure, it is available to both the calling page and the function.
The TotalInterest function
After changes to handle errors, the
example in
"A user-defined function
name!");
"A user-defined function
function looks like the following. Code that is changed from the
TotalInterest
example" on page 171 is in bold.
Last updated 8/5/2010
Description
Creates a simple form requesting you to enter your name.
Uses the script_name CGI variable to post to this page
without specifying a URL.
If you do not enter a name, the form posts an empty string as
the name field.
Defines a function to display "Hello name!" First, checks
whether the argument is an empty string. If so, displays an
error message.
Otherwise displays the hello message.
Returns the empty string. (The caller does not use the return
value). It is not necessary to use curly brackets around the if
or else statement bodies because they are single statements.
If this page has been called by submitting the form, calls the
HelloFriend function. Otherwise, the page just displays the
form.
example" on page 171, uses one
167

Advertisement

Table of Contents
loading

Table of Contents