Automating with Structures

I l @ ve RuBoard

Generally, it's a good idea to separate your programming code from your display code. In the case of the form to email comments, this would involve one file to process the form and one to display the confirmation. A problem arises, however. How do you pass the user information from one template to the other? You could pass each individual value through the URL, but there is a much easier way: use a structure.

We already have the information for both parts of the process, so now we'll split it up into two pages and add the tags to make them speak to each other.

  1. Cut all the HTML display code from formprocess.cfm.

    By removing all the HTML code, you have left only the code that ColdFusion needs to process the form. You can use the shortcut Ctrl+X.

  2. Create a new file by pressing Ctrl+N and paste (Ctrl+V) the removed HTML display code into the new document.

    This gives you a document that handles only the display of the confirmation message. Now you are ready to work on the formprocess template again.

  3. In formprocess.cfm, after the last <cfparam> tag add:

     <cfset userinfo = StructNew()> 

    In order to create a structure, you must use the StructNew() function. This sets up userinfo as a structure.

    You are now ready to enter values into the structure.

  4. After you create the structure, add the name of the user:

     <cfset bool = StructInsert(userinfo, "name", "#form.name#")> 

    NOTE

    This value could also be set by using the dot-notation userinfo.name=form.name to accomplish the same thing.

    This will create a key in the structure called "name" and assign it the value that was passed in the form variable called Name.

    You might be wondering why you are setting the variable bool. When the Structinsert function is run, it returns either Yes if the insert was successful or No if it was not. If the StructInsert was successful, then the variable bool will hold the value "Yes". If it was not, then the value of bool would be "No".

    NOTE

    Just like list variables , structures also have several ColdFusion functions to work with them. StructInsert() is just one of many. There are functions to remove items from a structure, clear it out altogether, copy it, find values contained in it, and so on. These functions can be accessed through the Expression Builder.

  5. Add the rest of the values to the structure:

     <cfset bool = StructInsert(userinfo, "email", "#form.email#")>  <cfset bool = StructInsert(userinfo, "comments", "#form.comments#")> <cfset bool = StructInsert(userinfo, "mailinglist", "#form.mailinglist#")> <cfset bool = StructInsert(userinfo, "interests", "#form.interests#")> <cfset bool = StructInsert(userinfo, "mailto", "#mailto#")> 

    NOTE

    Again, the dot-notation userinfo.mailinglist=form.mailinglist, userinfo.interests= form.mailinglist, and so on, would accomplish the same end result.

    This adds the rest of the form values, as well as the mailto variable. Make sure the mailto variable is inserted after the <cfif> block; otherwise the mailto variable will not exist.

    NOTE

    You may have noticed that you are also storing the list of user interests (that we set up in Lesson 7) in the structure. This is possible because structures can contain other complex variables, including other structures.

    These are all the values you will need for the confirmation page. You are now ready to use the values in your structure.

  6. Change the scope of any of the form variables that you have in the < cfmail > tag to reference the structure instead.

    Now all your values reference the structure. You could have left these values as the form variable, but you really need to have the values in the structure in order to pass them to the next template. Now you need a way to get to the confirmation page and display the proper variables.

  7. After the <cfmail> tag, add the following:

     <cfwddx action="CFML2WDDX" input="#userinfo#" output="userinfo"> 

    In order to be able to pass a complex variable through a URL, you must first serialize it. This is done using a technology called WDDX (see note below). You input the structure variable, and it outputs the serialized string to a variable of the same name. When that's done, you can pass this variable to the next template through the URL.

    NOTE

    ColdFusion provides built-in support for WDDX, which stands for Web Distributed Data Exchange. WDDX uses XML formatting style to serialize the structure and make it so you can pass it through the URL.

  8. After the <cfwddx> tag add:

     <cflocation url="formoutput.cfm?userinfo=#URLencodedformat(userinfo)#"> 

    You are using <cflocation> to send the user to another page and pass the userinfo variable through the URL. This way, when the user submits the form, the processing page is called and the formoutput template is shown. But this all looks like a seamless process to the user.

  9. Switch over to the new file you created to handle the HTML confirmation in step 2 and add the following at the top:

     <cfwddx action="WDDX2CFML" input="#url.userinfo#" output="userinfo"> 

    The first thing you have to do is deserialize the userinfo variable back into the structure. Once this is done, you can reference all the variables in the structure just as you did on the formprocess template.

  10. Rescope all your variables on the formoutput template that you created to reference the structure values.

    You can rescope the variables just as you did on the formprocess template earlier in this lesson. If you were to leave the scope on these variables as form, then you would get an error, as these variables no longer exist in the form scope.

  11. Save the new file in the Lesson8\Start folder as formoutput.cfm.

    Now you are ready to test your code and see how the structure and WDDX work.

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

    Make sure to fill in all of the information.

  13. Submit the form.

    You shouldn't see much of a change, except for the URL. The WDDX packet will appear in the URL and it will look a bit like a garbled mess. If you examine it closely, though, you will see each one of the elements of your structure hiding there.

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