3 Replies Latest reply on Apr 24, 2009 11:59 AM by Dan Bracuk

    Comma question with loops/form

      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

        • 1. Re: Comma question with loops/form
          Dan Bracuk Level 5

          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.

          • 2. Re: Comma question with loops/form
            Level 1

            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>

            • 3. Re: Comma question with loops/form
              Dan Bracuk Level 5

              On the topic of optimization, this

              <cfset formData=form[idx]>

              does the same as this

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

              only faster