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

How to handle commas within a List on web form?

Explorer ,
Apr 12, 2016 Apr 12, 2016

Copy link to clipboard

Copied

I have a web form, something like:

<form>

<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">

</form>

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.

Thanks!

How I'm looping:

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

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

      x=#i#<br />

</cfloop>

Views

1.4K

Translate

Translate

Report

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
Advocate ,
Apr 12, 2016 Apr 12, 2016

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

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 13, 2016 Apr 13, 2016

Copy link to clipboard

Copied

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.

HTH,

^_^

Votes

Translate

Translate

Report

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
Enthusiast ,
Apr 13, 2016 Apr 13, 2016

Copy link to clipboard

Copied

LATEST

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.

http://www.cflib.org/udf/formFieldAsArray

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.)

Votes

Translate

Translate

Report

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
Documentation