Adobe ColdFusion for the Web Developer

By Doug Hughes

Now, let’s look at some concrete features that illustrate this rather clearly.

Forms in ColdFusion

Rare is the HTML-based application that doesn’t make use of forms. ColdFusion applications are no different. What is different are the range of features ColdFusion provides to make it easier to write, populate, and validate forms.

Take a look at this block of code:

<cfform action="sendEmail.cfm">
<p>
<label for="name">Name:</label><br />
<cfinput type="text" name="name" required="true" message="Please provide
your name." />
</p>
<p>
<label for="email">Email Address:</label><br />
<cfinput type="text" name="email" required="true" validate="email"
message="Please provide a valid email address." />
</p>
<p>
<label for="message">Your Message:</label><br />
<cftextarea name="message" required="true" message="Please enter a
message to send."/>
</p>
<cfinput type="submit" name="Submit" value="Send Message" />
</cfform>
When run, the code above will output a simple form that looks like this in a web browser:

What’s not immediately apparent is that the form has JavaScript based validation that will require the user to provide all three fields and will insure that the email address provided is a valid email address. The setting the required attribute on the <cfinput>, <cftextarea> and other ColdFusion form tags is all you need to do to make the field required. The validate attribute is what is used to specify what type of data you require.

The message attribute on these tags controls the message that is shown if the value is not provided or not valid.

That was a fairly simple example of what <cfform> is capable of. Let’s think about some of the tedious things involved in creating a form. Consider the lowly select box. What if you have select box that needs to be populated from data in a database? Typically this is a pain in that you both need to query a database and generate the options in the select box. See how this is done with ColdFusion:

<cfquery name="users" datasource="myDatasource">
SELECT userId, name
FROM [User]
</cfquery>
<cfselect name="userId" query="users" display="name" value="userId" />
This results in a select box like the following:

As with other ColdFusion form tags, the select tag has built in validation capabilities. Now, what if you wanted to use a nice, JavaScript date selector? This is a pain in most languages because you’d need to go find a third party date selector to implement and follow its instructions to get it working. Either that, or you’d have to roll your own.

ColdFusion takes the complexity out of this in one line of code:

<cfinput type="datefield" name="startDate" />
This results in a nice text area with a calendar icon that, when clicked pops up a nice, easy to use date selector.

Ok, so that was simple. But, going back to the email form example above, what if I decided I wanted to use a browser based HTML editor?

All you need to do is set the richtext attribute of the cftextarea tag to true:

<label for="message">Your Message:</label><br />
<cftextarea richtext="true" name="message" required="true" message="Please
enter a message to send."/>
This adds a complete version of the FCKEditor to your document:

Sending Email from ColdFusion

ColdFusion makes sending email is extremely simple.   Let’s start by looking at how you can send a basic email from ColdFusion:
<cfmail from="dhughes@alagad" to="someone@somewhere.com" subject="Hello World">
This is a example email message!
</cfmail>
First, wrap your message in a <cfmail> tag. ColdFusion will queue the message and send it out via a very robust, high volume process. In my humble opinion this is already simpler than in any other language.

Now, let’s say you need to get a bit more complex with email. Perhaps your marketing department wants to send multipart mime formatted emails. Or, in English, you want to send one email with both HTML versions and text versions so the recipient’s mail client can decide which version to show. Unfortunately, other languages can make this a bit hard.  This is how it is with ColdFusion:

<cfmail from="dhughes@alagad" to="someone@somewhere.com" subject="Hello World">
<cfmailpart type="text/html">
<p>This is a <em>sample</em> email message!</p>
</cfmailpart>
<cfmailpart type="text/plain">
This is a example email message!
</cfmailpart>
</cfmail>
As you can see, the <cfmailpart> tag can be used to create multi part messages. In this example we have both HTML and Text versions.

And, it gets better! What if you wanted to send an attachment with your email? Simply use the <cfmailparam> tag and specify a file to send.

<cfmail from="dhughes@alagad" to="someone@somewhere.com" subject="Hello World">
<cfmailparam file="example.pdf" />
<cfmailpart type="text/html">
<p>This is a <em>sample</em> email message!</p>
</cfmailpart>
<cfmailpart type="text/plain">
This is a example email message!
</cfmailpart>
</cfmail>
The <cfmail> feature set is richer still. You can attach and send images embedded in the message, rather than referencing them via http. You can add you own mail headers too. And, as one other nice little feature, let’s say you have a set of people in a database you want to send customized emails to. Well, just use the <cfquery> tag to get the list of people and use the <cfmail> tag to send out a set of messages. This example shows how:
<cfquery name="recipients" datasource="myDatasource">
SELECT email, name
FROM [User]
</cfquery>
<cfmail query="recipients" from="dhughes@alagad" to="email" subject="Hello
World">
<cfmailparam file="example.pdf" />
<cfmailpart type="text/html">
<p>Dear #name#,</p>
<p>This is a <em>sample</em> email message!</p>
</cfmailpart>
<cfmailpart type="text/plain">
Dear #name#,
This is a example email message!
</cfmailpart>
</cfmail>
Note that we’re getting a query of users and that we specify the name of the query in the query attribute on the <cfmail> tag. That will cause the <cfmail> tag to iterate over the query and, for each row in the recordset, send an email. You will also note that the to argument on the <cfmail> tag specifies the email column in the query and that the email body itself is being personalized using the name provided in the query.

Come back next week, as we'll continue our look at Adobe's ColdFusion!




Page 2 of 2

Previous Page
1 2
 

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date