Differentiating Data with a List Variable

I l @ ve RuBoard

ColdFusion can look at a list variable and determine if a value is in that list. Coupling this ability with the <cfif> tag that you learned about in Lesson 7, you will be able to dynamically filter users and perform specific operations.

In this task, you will use a list variable to differentiate users based on their state code, filter the email that is generated using their form information, and route the email to the proper person based on the user 's state.

  1. Open X:\Lesson8\Start\basicform.cfm from your CD.

    This is the same form you have been working with in the previous lessons. Note that a fairly long select list containing an option for each state has been added to the form code.

  2. In the Inetpub\ wwwroot directory on your hard drive, create a new folder named Lesson8 and, inside it, a new folder named Start. Save your file.

    Once again, don't forget to move the related images and style sheet into that folder as well.

  3. Open a Web browser and type http://127.0.0.1/Lesson8/Start/basicform.cfm into the address window.

    You should now see that there is a select list from which the user can choose their state.

  4. Open X:\Lesson8\Start\formprocess.cfm.

    This file is the same as the formprocess file you have been working with in previous lessons.

  5. At the top of the page add the following:

     <cfset statelist = "MI,IN,OH,WI,KY,IL,TN,MN,IA"> 

    This creates a list of midwestern states. The actions you are about to complete will direct a user's emailed comments to a specific staff member if the user's state of origin is on that list.

  6. After the second <cfparam> tag add the following:

     <cfif ListContains(statelist, "#form.state#", ",") neq 0> 

    The ListContains function checks to see if the state the user selected in the form is on the list. If it is, the function will return the index value of where the state was; that is, its position in the list. If it is not on the list, the function returns the value 0 (zero).

    NOTE

    There are numerous functions in ColdFusion to work with list variables : functions to add and remove items, determine the length of the list, and many more. The Expression Builder has a list of all the functions available in ColdFusion. You can access the Expression Builder by right-clicking in the editor window and selecting Insert Expression from the contextual menu, or by using the keyboard shortcut Shift+Ctrl+E.

  7. Inside the <cfif> statement, add:

     <cfset mailto = "schmidt@hungrycow.com">. 

    You are setting the mailto variable to be used later in the < cfmail > tag. Of course, this address can be whatever you need it to be.

  8. After you create the first mailto variable, add:

     <cfelse> 

    You also need to create a mailto variable if the value is not on the list. You'll use the <cfelse> tag within the <cfif> tag to do so.

  9. Add the second mailto variable after the <cfelse> :

     <cfset mailto="info@hungrycow.com"> 

    Now if the user's state is not on the list, the user's email will be sent to this secondary address.

  10. Add your closing </cfif> tag.

    This ends your <cfif> block. The only thing left to do is make the to attribute of the <cfmail> tag use the mailto attribute instead of the hard-coded value.

  11. Change the to attribute to read:

     to="#mailto#" 

    Your <cfmail> tag will now route the mail to the proper person when the user submits the form. However, it would also be nice for the user to know to whom their email was sent!

  12. After the thank-yous in the confirmation area of the HTML below, add the following:

     Your email was sent to: #mailto# <br> 

    This will let the user know exactly where the email was sent. It will also let you know that your code is working properly.

  13. Save the formprocess template as Lesson8\Start\formprocess.cfm in your Inetpub\wwwroot directory.

    You are now ready to try out your form and see how it works.

  14. Open a Web browser and type http://127.0.0.1/Lesson8/Start/basicform.cfm into the address window.

    Fill out your information. Put in any state you want.

  15. Submit the form. Pay close attention to the email address that comes back in the confirmation message.

    If you selected one of the states in the list, the email should say it's been routed to schmidt@hungrycow.com. Otherwise it should go to info@hungrycow.com. Try it both ways to see the difference.

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net