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>
Reviewing the code
The following table describes the code:
Code
<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>
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
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.
Using UDFs effectively
213
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers