form submission

Hi All,
Its been a while since I was writing any forms in CF and I have what will probably be a simple problem for you to help me solve.

I have a basic form

<cfform action="/qpi/loadfactor_form2.cfm" name="myform"  height="1800"  >

<table >
<cfoutput query="loadfactor">
<cfparam name="comments" default="&nbsp;"   >
<td ><cfinput type="text" name="cons" value="#Container_cons_number#" readonly="yes" ></td>
<td ><cfselect query="cats" value="category" name="category" ></cfselect> </td>
<td ><cftextarea label="Comments" name="comments" required="yes"  cols="50"   value="#comments#" </cftextarea></td>
<tr><td colspan="8" align="right"><cfinput type="submit" name="submit" value="Submit"></td></tr>

This form is prepopulating some columns from a database and the user updates two corresponding columns with information.

I have an issue with the cftextarea field "comments".  I convert the list to an array on the second page.  I have inserted a space into the cftextarea so that the list always contains a value and does not fall out of sequence with the container number.  My concern is what if my users remove the space, thinking they are being helpful, I would update the wrong record.

So can I force the cftextarea list to always contain at least a space without showing it to the user.  So if the user does not type anything in that box can I insert a space ?
Who is Participating?
Agreed.  Looks like the bigger question is: "how to create a form for updating multiple records?"

I suspect the issue you're having is due to the fact that all of the form fields have the same name. So when the form is submitted you end up with one big CSV list of values for each field.  The problem with that is you need to use list functions to extract the individual values.  Unfortunately that's error prone.  Most CF list functions ignore empty elements. So if a user leaves a field blank, you'll end up with an uneven number of elements and your CF code will break The same will happen if a user enters a comma in one of the fields.

IMO, the better approach is use a counter number and create dynamic field names:  ie form.category1,form.category2, etc....

<cfform action="/qpi/loadfactor_form2.cfm" name="myform"  height="1800"  >
<cfoutput query="loadfactor">
    <cfinput type="text" name="cons#currentRow#" value="#Container_cons_number#" readonly="yes" ></td>
    <cfselect query="cats" value="category#currentRow#" name="category" ></cfselect>
    <cftextarea label="Comments" name="comments#currentRow#" required="yes"  cols="50"   value="#comments#" </cftextarea>

<input type="hidden" name="totalFields' value="#loadfactor.recordCount#">
<cfinput type="submit" name="submit" value="Submit">

Use a loop to extract the values and do whatever you need to do with them:

<cfloop from="1" to="#form.totalFields#" index="row">
         <!--- get field values --->
         <cfset category = FORM["category"&  row]>
         <cfset comments= FORM["comments"&  row]>
         <cfset cons = FORM["cons"&  row]>
         ....  do something with values ....

I think we should solve the problem without the space work-around.   How is having a space or not having a space going to update the wrong record in your database?   That sounds like a problem that should be fixed.

I don't really understand what the space is doing - what problem is it solving?

You mention you are converting "the list" to an array, but never mention where this list comes from.. are the users typing a list of things into the cftextarea?    

It seems like you have a problem which you are using this space to workaround, so let's talk about the problem and not the space?
Good catch agx, I didn't recognize it at as multiple record form, but that would certainly fit the issue.   I agree with the approach of adding a counter to the input form names as the best solution.

The asker also needs to keep in mind that the same situation will occur if someone enters a comma in the text or name fields.   That would also break the form.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

>> if someone enters a comma in the text or name fields.   That would also break the form.

Yep.  The same name field approach is very brittle.  At least up until CF9 :) I forgot that CF10  introduced a new feature. It let's you control whether "same-named-fields" are passed as CSV lists or arrays.  If you enable this setting your Application.cfc:

          this.sameFormFieldsAsArray = true;

.. all fields with the same name would be passed as arrays instead.
cool!  I didn't know about that..
lokinsella1Author Commented:
Many thanks for the feedback, I used this solution to solve the problem
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.