2

I have a form that allows the user to add co-authors so I'm trying to loop through those fields (if they exist) but can't seem to be able to get the values. "coauthorNo" is a hidden input field that only exists if the user adds 1 or more co-authors. I'm testing with a cfoutput in my page to see if I can get the values but no luck thus far. This is running on a Coldfusion 10 server.

<cfif IsDefined("FORM.coauthorNo")>    
  <cfset coAuthCount = listLen("#FORM.coauthorNo#", ",")>

            <cfloop from="0" to="#coAuthCount#" index="i">
                <cfset CoAuthF = "#FORM['CoAuthFirstName'&i]#">
                <cfset CoAuthL = "#FORM['CoAuthLastName'&i]#">

                <cfoutput>CoAuth's: #CoAuthF#, #CoAuthL#,</cfoutput>
            </cfloop>
</cfif>

Edit: Changed the cfloop from value to 1 and that fixed it. My dynamically created form fields started at 1 vs 0. ie CoAuthFirstName1

TheRanch
  • 23
  • 7
  • This looks good.. can you share your form code, both the cf stuff and the rendered stuff? What happens when you dump `#form.fieldnames#` when you have some co-authors set? – Regular Jo Jan 08 '15 at 15:48
  • 1
    Step 1 - look at your data. cfdump your form scope. – Dan Bracuk Jan 08 '15 at 15:51
  • 2
    What error are you receiving? What doesn't work that you are expecting to work? You haven't told us the names of your form fields. Without that information how can we help you? – Brad Wood Jan 08 '15 at 15:57
  • 1
    Agreed, you need to post more details about how your code is working differently than you expect. *RE: cfdump your form scope.* .. and please update your question with the contents of the cfdump. – Leigh Jan 08 '15 at 16:03
  • @BradWood The page doesn't work when I get an error. The form fields are listed in my code "CoAuthFirstName" & "CoAuthLastName". There are more fields but for the sake of this question I'm only showing two fields in the loop. Also in the generated html code the field names are "CoAuthFirstName1" & "CoAuthFirstName2" if you add two co-authors. – TheRanch Jan 08 '15 at 16:03
  • 1
    Sorry guys...I realized what was the problem. My cfloop starts at 0 when the names of the fields start at 1. ie CoAuthFirstName1 When I change the loop to start at 1 I get the output I'm looking for in the code sample. – TheRanch Jan 08 '15 at 16:07
  • 2
    @TheRanch there's no need for the `"#` or `#"` in your cfset statement – Matt Busche Jan 08 '15 at 16:23
  • @MattBusche righty. those are variables so no quotes or hash needed for that. So provided there is a string or number in there, it'll just set to your new variables as such. – Frank Tudor Jan 08 '15 at 16:39
  • Sorry @MattBusche and Frank Tudor, the code doesn't work without them. – TheRanch Jan 08 '15 at 16:48
  • @TheRanch - No, the cfset will work fine as long as you remove both the pound signs *and* quotes. You do not actually need `#` signs as often as you might think. – Leigh Jan 08 '15 at 16:50
  • When you take a variable and wrap it in hashes, and then wrap that in quotes you just cancelled the hashes out. Just skip both. is the same as You need to actually paste in the ACTUAL ERROR MESSAGE you're getting. Telling us it "doesn't work" doesn't help us. Chances are the error was something like "variable CoAuthFirstname0 does not exist" which would have helped. – Brad Wood Jan 08 '15 at 19:29
  • CFML 101: http://blog.adamcameron.me/2013/09/when-to-use-pound-signs.html. Get a handle on that sort of thing before worrying too much about how to do more complicated things in CFML. – Adam Cameron Jan 08 '15 at 19:57

1 Answers1

2

FYI there's no need for all this rigamarole. You can loop through a list easily.

<Cfset lCount = 0/>    
<cfif IsDefined("FORM.coauthorNo")>    

                <cfloop list="#form.coauthorNo#" index="i">
                    <cfset CoAuthF = FORM['CoAuthFirstName' & lCount]>
                    <cfset CoAuthL = FORM['CoAuthLastName' & lCount]>

                    <cfoutput>CoAuth's: #CoAuthF#, #CoAuthL#,</cfoutput>
                    <Cfset lCount++/>
                </cfloop>
    </cfif>

And get in the habit of excluding the pound signs when you are not outputting stuff to the page or passing an attribute to a tag like cfloop. It makes for cleaner code.

Mark A Kruger
  • 7,183
  • 20
  • 21
  • Thanks Mark... We were definitely rigamaroling. +1 – Frank Tudor Jan 08 '15 at 17:03
  • Ha - well everyone was saying it so I just wrote it down :) – Mark A Kruger Jan 08 '15 at 17:06
  • Except that this doesn't work in my setting. Thanks for the input though. – TheRanch Jan 08 '15 at 18:00
  • (Edit) Can you elaborate on how it "doesn't work"? What does the `FORM.coauthorNo` list contain? A list of id's, rather than sequential numbers, perhaps? – Leigh Jan 08 '15 at 18:43
  • I see where you have the problem - though I should think that _you would be able to figure it out_ given the example. I have added a loop counter for you. – Mark A Kruger Jan 08 '15 at 19:17
  • True, though I am still curious about the contents of the list... It seems strange to have what appears to be a list of "coAuthor" id's with no direct relationship to the "CoAuthLastName" and "CoAuthFirstName" fields. If the two are related, I would have expected a link between the two. For example, by using the ID as part of the first/last field name. – Leigh Jan 08 '15 at 20:27
  • Yes... it won't work unless he orders the list in his hidden field so he knows for sure which is which. It's easier just to append the ID to the form fields for the names themselves. – Mark A Kruger Jan 08 '15 at 20:42
  • Yep. Honestly, there are several ways that kind of loose binding and list handling can cause problems. That is why I brought it up. Though my questions/comments were really directed at the asker (since I know *you* know it already, but I was not sure if *they* did ;-) – Leigh Jan 09 '15 at 01:47
  • @Leigh I am by no means a CF expert which is why I asked this question. I adopted a site and am picking up the pieces. This issue was hopefully the last I need to "fix". The original code in this form processing page had a loop similar to what you suggested but that didn't work (orig & yours) as Mark pointed out. I didn't modify the original form html either. I'm always looking to learn and write cleaner code so thanks for the input. – TheRanch Jan 09 '15 at 22:00