3 Replies Latest reply on Apr 13, 2016 6:03 PM by Jamo

    How to handle commas within a List on web form?

    benhenny Level 1

      I have a web form, something like:



      <input type="text" name="objectives" value="This is a test">

      <input type="text" name="objectives" value="And another, if you know what I mean.">

      <input type="text" name="objectives" value="And yet another.">

      <input type="submit" name="btnGo" value="Do It">



      And an action page that submits it to a db. The trouble is, the objectives will have commas in them, occasionally, and CF turns each comma into a list item, so my loop looks like:

      1. This is a test
      2. And another
      3. if you know what I mean
      4. And yet another


      but I want:

      1. This is a test
      2. And another, if you know what I mean
      3. And yet another


      What I want to do is loop through the objectives, retaining the commas within the elements. I've been struggling with this for days. I want to avoid having to write some replace method in JS if I can help it.




      How I'm looping:

      <cfset values = ListToArray(#FORM['objectives']#)>

      <cfloop index="i" list="#FORM['objectives']#">

            x=#i#<br />


        • 1. Re: How to handle commas within a List on web form?
          Steve Sommers Level 4

          For me, if they were radio or check boxes, I would avoid using commas as values. In text and hidden fields, as in your example, I would uniquely name the fields to avoid worrying about it. Another problem you'll eventually run into is that pre-CF11, by default, passes duplicated named fields differently than CF11+. CF11+ passes the values as array an array instead of a list which actually solves your problem as it's no longer a comma separated list -- it's an array (again, by default).

          • 2. Re: How to handle commas within a List on web form?
            WolfShade Level 4

            Hi, benhenny,


            As Steve Sommers pointed out, CF11 (and most likely later versions) will pass multiple-named inputs (and I'm assuming multi-select) as an array by default (it can be switched to submitting a list in CFAdmin).


            So.. to be sure that what you want to do is compatible with all versions of CF is to first check for the existence of the form element, then check to see if it's an array (if not, it's a list.)


            If it's an array, you're good to go.  If it's a list, then convert to an array, and go from there.  You could use JavaScript on the client-side to convert commas entered by the user into their HTML entity version (&#44; will show up in browser as a comma.)  I know you wanted to avoid that, but it's not terribly difficult, and the only way I can think of to avoid user-entered commas from destroying the delimitations of your list.


            If you don't want the HTML entities to be entered into a database or email, then (depending upon what CF version you are running) you can use canonicalize() to correct the input just prior to whatever final processing you have in mind.





            • 3. Re: How to handle commas within a List on web form?
              Jamo Level 2

              We do this all the time... especially when using an advanced configuration form and new fields are added on-the-fly with user-provided content and you can't really prevent commas from being submitted in the fields.


              To work around this, use the formFieldAsArray UDF.



              Here's the description:

              "When you pass several form or URL variables into ColdFusion with the same name, they end up as a comma separated list. This is commonly done with checkboxes - a user can check as many items as they want, then they will end up in your code all in a single variable. This works fine, until your data contains a comma. This function will return the data as an array to get around that problem. Tested on ColdFusion 8 and 9, probably runs on CF 7 also, maybe even 6. UPDATE - rewritten to work in CF10. Code is much more simple now."

              NOTE: If using this with checkboxes or radio buttons that may not be checked, be sure to check to see if the parameter exists first as nothing checked will result in no parameter being passed back to the server.  (We usually add a blank text field with the same field name right before the first checkbox/radio field to overcome this potential issue.)