| Contents > Developing ColdFusion MX Applications > Writing and Calling User-Defined Functions > Using UDFs effectively > Handling errors in UDFs Displaying error messages |
|
|
|
|
||
Your function can test for errors and use the WriteOutput function to display an error message directly to the user. This method is particularly useful for providing immediate feedback to users for simple input errors. You can use it independently or in conjunction with either of the other two error-handling methods.
For example, the following variation on a "Hello world" function displays an error message if you do not enter a name in the form:
<cfform method="POST" action="#CGI.script_name#">
<p>Enter your Name:
<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 name!");
else WriteOutput("Hello " & name &"!");
return "";
}
if (IsDefined("Form.submit")) HelloFriend(Form.name);
</cfscript>
The following table describes the code:
Code |
Description |
|---|---|
<cfform method="POST" action="#CGI.script_name#"> <p>Enter your Name: <input name="name" type="text" hspace="30" |
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. |
<cfscript>
function HelloFriend(Name) {
if (Name is "") WriteOutput("You forgot your |
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 braces 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. |
|
|
||
| Contents > Developing ColdFusion MX Applications > Writing and Calling User-Defined Functions > Using UDFs effectively > Handling errors in UDFs Displaying error messages |
|
|
ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting
Version 6.1
Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.