[thelist] Tip/CFSwitch/MultiForms in one file

Palyne Gaenir palyne at sciencehorizon.com
Fri Jul 7 17:18:53 CDT 2000


<tip type="CFML" level="intermediate" reason="IO">

When using the CFSwitch tag in CF, you are setting different parts of 
the file to be viewed depending on the parameter passed. 

The switch tag takes a given parameter name and value, and then 
assigns another parameter name and value (one can simplify this).  I 
use FormType as my incoming param/value, and I have the switch set it 
to a numbered PageType param/value.

Despite the final 'default' sequence in the switch code (see below), 
this does NOT mean that any access of the page will default to that 
paramvalue.  It means that if the paramvalue is not recognized by the 
CFSwitch, it will then use the default.  However, there must BE a 
paramvalue or the file will not come up -- you'll get an error.

To ensure that the file can be called from its simple name (from your 
passworded admin menu...), add a CFPARAM right above the CFSwitch tag 
which sets a default.  For example:

<CFPARAM NAME="FormType" DEFAULT="menu">

An ordinary switch sequence in CF might look like this:

<CFSwitch Expression="#FormType#">
<!--- menu for admin --->
<CFCase Value="menu"><CFSET PageType = "1"></CFCase>
<!--- add record --->
<CFCase Value="add_data"><CFSET PageType = "2"></CFCase>
<!--- edit record --->
<CFCase Value="edit_data"><CFSET PageType = "3"></CFCase>
<!--- insert record --->
<CFCase Value="insert_data"><CFSET PageType = "4"></CFCase>
<!--- update record --->
<CFCase Value="update_data"><CFSET PageType = "5"></CFCase>
<!--- confirm delete record --->
<!--- not shown: delconfyes, for separate file --->
<CFCase Value="delete_conf"><CFSET PageType = "6"></CFCase>
<!--- list all records --->
<CFCase Value="display_all"><CFSET PageType = "7"></CFCase>
<!--- display one record --->
<CFCase Value="display_one"><CFSET PageType = "8"></CFCase>
<!--- default page, which is admin menu page for this module --->
<CFDefaultCase> <CFSET PageType = "1"> </CFDefaultCase>
</CFSwitch>

That's a simple CFSwitch sequence.  The CFPARAM sets the default to 
menu so you can call the file directly without a FormType value.  The 
note about delconfyes in the switch refers to the fact that all my 
deleting of records is done in a separate file (used for that only). 
(One hard lesson I won't learn twice. ;-))
 
Within the body of your page you would set IF tags around the content 
for each page, such as:

<CFIF PageType IS "1">
All the content for your 'add' form would go here
</CFIF>

You can also break the page up into multiple pieces as a 'reusable 
form' as Forta refers to them, but frankly, this confuses the crap 
out of me when it comes to debugging a 20-form switched file with 
multiple cfoutputs and cfifs all over the place. But, that's me, I'm 
a manager by trade not a programmer, even CF sometimes makes my brain 
hurt and it's EASY.  For most devel people it is probably a breeze. 
(I don't like doing the forms separately initially, which is the 
obvious answer to that.)  

Put your CFPARAM and CFSwitch at the top of the page.  If you put 
your html head stuff below that, and THEN start your switching, and 
then put your navigation/footer below all that, the head/nav/footer 
will be on all the forms, since they are not within an IF statement.

</tip>
--------
Palyne Gaenir
ScienceHorizon Web Media
http://www.sciencehorizon.com
palyne at sciencehorizon.com
Toll-Free 877-316-0763




More information about the thelist mailing list