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

Comma question with loops/form

Community Beginner ,
Apr 24, 2009 Apr 24, 2009

Hi all-

I have a problem with commas. I use a loop to create a set of <cfinputs> all with the name "theData". Thus when you enter some data in the input boxes and hit submit, the dump shows the contents of "theData" as the contents of each <cfinput> separated by a comma.

The real form can have a couple hundred of these inputs, which is why I used the one name (I pass the whole thing off to the database to dissect it).

Great, except that I don't know what to do if the user enters data with a comma in one of the <cfinputs>...in the dump now the fields are co-mingled with the data, and I can't tell what is 'data' and what is a field. That has to have come up enough that there's probably a built-in solution that I'm not grokking...is there a way to properly separate the individual fields so that I can tell what is truly data, regardless of what the user puts in the field?

<cfif isDefined("Form.saveStuff")>
    <cfdump var="#Form#" label="Form Variables">
</cfif>
<html>
<body>
    <cfform>
        <cfloop index = "LoopCount" from = "1" to = "5">
            <cfinput size="4" style="background-Color:##66FF00" type="text" name="theData"/>
            <br/>
        </cfloop>
        <cfinput name="saveStuff" type="submit" value="Save It" />           
    </cfform>
</body>
</html>

Thanks,

Ron

TOPICS
Advanced techniques
479
Translate
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 ,
Apr 24, 2009 Apr 24, 2009

In your form, give your inputs different names.  You can still use a loop.

<cfloop index = "LoopCount" from = "1" to = "5">
            <cfinput size="4" style="background-Color:##66FF00" type="text" name="theData#loopcount#"/>
            <br/>
        </cfloop>

When you process the forms, do something like this

<cfloop list = "#form.fieldnames#" index = "idx">

<cfif left(idx, 6) is "theData">

do something.

Translate
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 Beginner ,
Apr 24, 2009 Apr 24, 2009

Hi Dan-

Thanks for the tip, it helped a lot.

In case anyone has a similar question, here's the code I came up with to test out Dan's idea. It's not exactly optimized, but I think it gets the point across:

<cfif isDefined("Form.saveStuff")>
    <cfdump var="#Form#" label="Form Variables">
    <cfset theValue=""/>
    <cfloop list = "#form.fieldnames#" index = "idx">
        <cfif left(idx, 7) is "theData">
            <cfset formData=#Evaluate('Form.#idx#')#>
            <cfset theValue=#theValue# & "||" & #formData#>
        </cfif>
    </cfloop>
    <cfoutput><br/>The result is #theValue#</cfoutput>
</cfif>
<html>
<body>
    <cfform>
        <cfloop index = "LoopCount" from = "1" to = "5">
            <cfinput size="4" style="background-Color:##66FF00" type="text" name="theData#loopcount#"/>           
            <br/>
        </cfloop>
        <cfinput name="saveStuff" type="submit" value="Save It" />           
    </cfform>
</body>
</html>

Translate
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 ,
Apr 24, 2009 Apr 24, 2009
LATEST

On the topic of optimization, this

<cfset formData=form[idx]>

does the same as this

<cfset formData=#Evaluate('Form.#idx#')#>

only faster

Translate
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