Skip to main content
Known Participant
February 6, 2010
Question

CFLoop through form fields problem

  • February 6, 2010
  • 1 reply
  • 6143 views

Hi all,

Maybe someone may know this answer but it's driving me nuts.

I am looping through form fields from another page and trying to gather form field name values from dynamic checkboxes.

I want to extract the ID values built into the names...and delete those records.

Each checkbox name begins with "img_". (For example : img_59)

This is the code I am using :

----------------------------------------------------------------------------------------------------

<cfloop index="i" list="#Form.FieldNames#" delimiters=";">

<cfif LEFT(i, 4) IS "img_">
<CFSET selectImageID = RemoveChars(i, 1, 4)>
<CFSET selectImageID = #Evaluate(selectImageID)#>
</cfif>

<!--- Delete Query for selected image IDs --->
<cfquery name="deleteImages" datasource="ctmprojects">
DELETE
FROM photos
WHERE ID = #selectImageID#
</CFQUERY>
</cfloop>

<cflocation url="edit.cfm?ID=#Form.ID#">

----------------------------------------------------------------------------------------------------

The form has one hidden field with an "ID" value.... but most are dynamic checkboxes that have names that start with "img_".

I need to loop through these to capture the Record numbers of the images to delete.

The problem is : it does not seem to be looping a single name value at a time....

If I do a <cfoutput>#i#<br></cfoutput>  at the beginning or end of the loop I get a full list of form values each time ... such as...

ID,IMG_61,IMG_63

This is causing it to not evaluate the first 4 characters of each.

The code works if the form only has one checkbox because the result is one value... but with multiple form fields it's always one long string with the delimiters...

Does anyone have a clue as to what is going on?.... Or is there a better method of doing this?

Thanks so much

Tim Roman

    This topic has been closed for replies.

    1 reply

    Inspiring
    February 6, 2010

    Hint: what's the list delimiter of form.fieldnames, and what delimiter are you actually using...?

    --

    Adam

    tromanNMVAuthor
    Known Participant
    February 6, 2010

    Hi Adam,

    I tried using a comma as the delimiter first.......  but that failed to work also. Sorry the example shows a semi-colon.... But, it is now a comma again in my code. However, when I use a comma it still does not loop through individual names... If I <cfoutput> the value of the list inside the loop.... it's a list of all the values separated by a space....?

    Thanks for your response though..

    Tim

    Inspiring
    February 6, 2010

    If you just have this:

    <cfloop index="i" list="#Form.FieldNames#" delimiters=";">

    <cfoutupt>#i#<br /></cfoutput>

    </cfloop>

    What do you see?

    Also, is the value that selectImageID holds the name of another form field?  If that's the case you can ditch the evaluate() call and just use form[selectImageID].  It's better form (no pun intended).

    --

    Adam