Building multi-step forms
Bookmark and Share
 

Posted by Rahul

We at most of the times are required to build multi-step forms, involving passing on the form data from 1 page to the other.

I generally use this small UDF to include the form elements from the previous page. It also has the argument which can be used to exclude certain fields to be omitted. All the form fields except the special "fieldnames" and the one's in the exclude list will be passed as hidden fields to the next page. Here is the UDF code

<cffunction name="createHiddenFields" output="no" returntype="string">
  <cfargument name="excludeFields" type="string" default=""/>
  
 <cfscript>
  var formFieldsList ="";
  var formFieldArray= ArrayNew(1);
  var formHTML="";
  var element=0;
  var exclude = listappend(arguments.excludeFields,"formfields");
  
  if(structKeyExists(form,"fieldnames")) {
   formFieldsList = form.fieldnames;
  }
  formFieldArray = listtoArray(formFieldsList);
 
 </cfscript>

 <cfsavecontent variable="formHTML">
   <cfoutput>
 <cfloop from ="1" to="#ArrayLen(formFieldArray)#" index="element">
   <cfif listfindnocase(exclude,formFieldArray[element]) eq 0>
     <input type="hidden" name="#lcase(formFieldArray[element])#"  value="#htmleditformat(form[formFieldArray[element]])#">
  </cfif> 
   </cfloop>
 </cfoutput>
 </cfsavecontent> 
 
  <cfreturn formHTML />
</cffunction> 

To use the UDF just call this udf anywhere within the <form>...</form> in your page

You might also find similar functionality implemented as a custom tag on dans.blog

1 comments

Rahul,

The one reason I went w/the custom tag approach was so that I could have it automatically parse the form fields for me.

I started off which a UDF solution, but quickly realized that I did want duplicate form fields copied. Many of the pages in our app can come from various different pages, so it got really messy quickly keeping an up-to-date list of fields to exclude.

By moving to a "Custom Tag", I'm able to parse the content between the open/close to automatically exclude fields included in the current form from being included.

There are still times when a custom tag is the best solution for a problem, and this, at least for my situation, was one of them.

-Dan

Post a Comment

About Me

My Photo
Rahul Narula

Application Architect Webteam@Adobe

View my complete profile

Subscribe via email

Enter your email address:

Live Traffic Map

  Powered by Blogger

Twitter

Archives