print banner
 

Basic Programing and Logic II for Beginning ColdFusion Programmers

Sign on the Dotted Line

In ColdFusion the primary method of getting data from the user is a plain HTML form, or a CFform. I prefer the HTML form because it passes w3c validation, where are the CFform doesn't. Granted that CFform does do user input validation for you, but it also tends to generate more code than simple HTML form. So, let's quick write a form both ways. We'll assume the document is called "name.cfm" and it submits to itself. First, the HTML version:

<form action="name.cfm" method="post"> Email:<input type="text" size="15" name=email" /> <br/> First Name:<input type="text" size="15" name="fname" /> <br/> Last Name:<input type="text" size="15" name="fname" /> <br/> <input type="submit" name="submit" value="Go!" /> </form> See the form

Second, the CFForm version:

<cfform action="name.cfm" method="post" type="html"> Email:<cfinput type="text" size="15" name="email" required="yes" validate="required" validateat="onsubmit" /> <br/> First Name: <cfinput type="text" name="fname" size="15" required="yes" validate="required,email" validateat="onsubmit"/> <br/> Last Name: <cfinput type="text" size="15" name="lname" required="yes" validate="required" validateat="onsubmit"/> <br/> <cfinput type="submit" name="submit" value="Go!" /> </cfform> See the form

The first one is small,compact and does the basics, the second generates a lot of JS code for validation but does warn the user to enter the required fields.In either case,we get the data from the user, which was the primary goal. Now that we have the input, let's see how to use it.

So, once we get the data we want to do something like email them a letter like: Dear <cfoutput>#form.fname# #form.lname#</cfoutput>, How are you today. To do this we simply add a cfmail tag with the proper information: <cfmail to="#form.email#" from="jastreich@example.com" subject="Test mail"> Dear <cfoutput>#form.fname# #form.lname#</cfoutput>, How are you today. </cfmail>

Lastly, we need to put the pieces together by looking for the submission using the conditional statements we learned last time. so the full contents of the body tag of name.cfm would be:

<cfif isdefined(`submit')> <cfmail to="#form.email#" from="jastreich@example.com" subject="Test mail"> Dear <cfoutput>#form.fname# #form.lname#</cfoutput>, How are you today. </cfmail> <cfelse> <cfform action="name.cfm" method="post" type="html"> Email:<input type="text" size="15" name=email" required="yes" validate="required" validateat="onsubmit"/> <br/> FirstName:<cfinputtype="text"name="fname" size="15" required="yes" validate="required,email" validateat="onsubmit"/> <br/> LastName:<cfinputtype="text"size="15" name="lname" required="yes" validate="required" validateat="onsubmit" /> <br /> <cfinputtype="submit"name="submit"value="Go!" /> </cfform> </cfif>

One last note, the method on the cfform and form tag were both post. As you may already know, this means that the information is past "invisibly" and the data returned is in the form scope (#form.email# and the like). If instead we'd set the method equal to get in the forms, then the scope would have been URL and it should have ended up appearing at the end of the URL in the following manner:

http://example.com/name.cfm&email=some@ email.com&fname=Joe&lname=Blow