• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

cfloop through cfform variables

I'm trying to simplify the following code (which works):
<cfif NOT Len(FORM.MobilePhone)>
	<cfset FORM.MobilePhone = "Not given">
<cfif NOT Len(FORM.HomePhone)>
	<cfset FORM.HomePhone = "Not given">
<cfif NOT Len(FORM.StreetAddress)>
	<cfset FORM.StreetAddress = "Not given">
<cfif NOT Len(FORM.Suburb)>
	<cfset FORM.Suburb = "Not given">
<cfif NOT Len(FORM.ZipCode)>
	<cfset FORM.ZipCode = "Not given">
<cfif NOT Len(FORM.Country)>
	<cfset FORM.Country = "Not given">
<cfif NOT Len(FORM.OtherComments)>
	<cfset FORM.OtherComments = "Not given">

Open in new window

by trying this:
	<cfloop index="ListItem" list="Mobilephone,HomePhone,StreetAddress,Suburb,ZipCode,Country,OtherComments">
		<cfif NOT Len(FORM.#ListItem#)>
  		<cfset FORM.#ListItem# = "Not given">

Open in new window

which gives me the error message:
A CFML variable name cannot end with a "." character.
The variable FORM. ends with a "." character. You must either provide an additional structure key or delete the "." character.
How can I get this to work?
  • 2
1 Solution
You need to use array notation

ie  <cfif NOT Len(FORM[ListItem])>
         <cfset FORM[ListItem] = "Not given">
> <cfoutput>

Btw: You don't need cfoutput for that section of code.  It'll work just fine without it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now