• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Soo close. Looping over certian form elements to see if they are empty

Explorer ,
Oct 13, 2010 Oct 13, 2010

Copy link to clipboard

Copied

I have a form that gets split into 2 actions. Part is emailed, part is databased. Didn't think of it at the time, since the part of the form is shared, no matter if the parts that are databased are empty, it still gets inserted. What I need to do is check about 11 fields to see if they are blank. If not, then do an insert, if so, then don't do anything.

What I thought would work best was create an array of the fields that I wanted checked. Then loop over the form.(element) and see if it is not blank. If it isn't, then do the insert and stop checking. I tried this, but am getting errors. Any ideas? Would one long cfif statement with a bunch of OR's be better?

<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# />
        <cfif form.#fname# NEQ "">
            Do and insert
            <cfbreak />
        </cfif>
    </cfloop>

TOPICS
Advanced techniques

Views

721

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 13, 2010 Oct 13, 2010

Copy link to clipboard

Copied

I do stuff like that like this:

<cfloop list = "#form.fieldlist#" index= "ThisField">

<cfset ThisValue = form["#ThisField#">

Then apply whatever logic is necessary.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Oct 13, 2010 Oct 13, 2010

Copy link to clipboard

Copied

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]>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2010 Oct 15, 2010

Copy link to clipboard

Copied

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>
        <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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Oct 22, 2010 Oct 22, 2010

Copy link to clipboard

Copied

LATEST

     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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation