-
1. Re: Soo close. Looping over certian form elements to see if they are empty
Dan Bracuk Oct 13, 2010 7:57 AM (in response to wmkolcz)I do stuff like that like this:
<cfloop list = "#form.fieldlist#" index= "ThisField">
<cfset ThisValue = form["#ThisField#">
Then apply whatever logic is necessary.
-
2. Re: Soo close. Looping over certian form elements to see if they are empty
ilssac Oct 13, 2010 8:10 AM (in response to Dan Bracuk)Dan Bracuk wrote:
<cfset ThisValue = form["#ThisField#"]>
You can even get rid of a few characters if you like. This works just as well.
<cfset ThisValue = form[ThisField]>
-
3. Re: Soo close. Looping over certian form elements to see if they are empty
BKBK Oct 15, 2010 3:50 PM (in response to wmkolcz)Two things. First, from what you've described ("check about 11 fields to see if they are blank. If not, then do an insert, if so, then don't do anything"), there should be no <cfbreak>. Your code should read like
<cfset ffields = ArrayNew(1) />
<cfset ffields[12] = "degree" />
<cfset ffields[1] = "title" />
<cfset ffields[2] = "dept2" />
<cfset ffields[3] = "spec" />
<cfset ffields[4] = "location" />
<cfset ffields[5] = "addr" />
<cfset ffields[6] = "city" />
<cfset ffields[7] = "zip" />
<cfset ffields[8] = "phone" />
<cfset ffields[9] = "medschool" />
<cfset ffields[10] = "baordcerts" />
<cfset ffields[11] = "otherChanges" /><cfloop index="x" from="1" to="#arrayLen(ffields)#">
<cfset fname = ffields[x]>
<cfif trim(form[fname]) NEQ "">
Do and insert
</cfif>
</cfloop>Secondly, I hope you do realize you've assumed that all the fields you've mentioned will be present in the submitted form. Otherwise, you will get an error.
In fact, if they are, and if you wish to consider every field in the form, then you can simplify the above code to something like
<cfloop collection="#form#" item="field">
<cfif trim(form[field]) NEQ "">
Do and insert
</cfif>
</cfloop> -
4. Re: Soo close. Looping over certian form elements to see if they are empty
insuractive Oct 22, 2010 2:26 PM (in response to BKBK)Assuming that you know what the fieldnames are at design-time, you can save some vertical space (and some maintenance time down the road) by
storing all your fields as a list instead of an array:
<cfset variables.sReqFields = "title,dept2,spec,location,addr,city,zip,phone,medschool,baordcerts,otherChanges,degree">
The following code will loop over your specified fields and allow you to execute code for EACH non-empty fields encountered
<cfset variables.sReqFields = "title,dept2,spec,location,addr,city,zip,phone,medschool,baordcerts,otherChanges,degree">
<cfloop list="#variables.sReqFields#" index="sFieldName">
<cfif isDefined("FORM.#sFieldName#") AND Trim(FORM[sFieldName]) neq "">
FIELD IS NOT EMPTY - DO ADDITIONAL PROCESSING ON FIELD HERE
</cfif>
</cfloop>
However, in your orignal post you mentioned that you want to check to see if all 11 fields were completed and if so, do your insert. That would look something like this:
<cfset variables.sReqFields = "title,dept2,spec,location,addr,city,zip,phone,medschool,baordcerts,otherChanges,degree">
<cfset variables.bFieldsCompleted = true>
<cfloop list="#variables.sReqFields#" index="sFieldName">
<cfif NOT isDefined("FORM.#sFieldName#") OR Trim(FORM[sFieldName]) eq "">
<cfset variables.bFieldsCompleted = false>
</cfif>
</cfloop>
<cfif variables.bFieldsCompleted>
DO YOUR INSERT HERE.
</cfif>
Then if you needed to modify the list, you could add or remove elements fairly easily.


