#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    45
    Rep Power
    6

    Loop over form fields generated by JS


    I have a form set up inside of a table with 4 columns. Let's call these columns ID, SSN, MemberType, and ZipCode. The first 3 columns are dropdown boxes pre-populated with results from 3 queries. These are also the primary key in the concerned DB table.

    The 4th column - ZipCode- is a user-editable text box.

    Each row in the table consists of these 4 fields.

    I begin by giving just one row in the table. Then, using JavaScript, I give the user the ability to add more such rows to the table on the fly.

    Now my question is that since more form fields can be potentially generated after the page has been served, how would I, on submitting the form, use the form data to update the database?

    The drop-downs and the text box in the first row are called 'idRow1', 'ssnRow1', 'membertypeRow1', and 'zipCodeRow1'.
    Subsequently, on each generation of a new row, the fields are appended with the row#. i.e. idRow2, ssnRow2, memberTypeRow2 and zipCodeRow2. And so on.

    Anyone know how to proceed?

    Thanks!
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,264
    Rep Power
    968
    You can either pass a hidden form field associated with each set of fields that states how many there are (i.e. if you have ssn1 and ssn2, you have a hidden field called ssnCount=2). Then you can loop over the form.fieldNames variable when you submit it to the server and process each one.

    Or you can use something like this: http://formutils.riaforge.org/, which lets you use array-like syntax in your field names and they will be converted into arrays and structures automatically on the server, which you can then process however you like.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    84
    Rep Power
    10
    I find ListToArray(form.FIELDNAMES) to be quite helpful in processing some of my more complicated forms. You can then loop over every form field that was submitted and handle it accordingly.

    For example:

    Code:
    <cfloop index="i" from="1" to="#ArrayLen(fields)#">
    		<cfif fields[i] contains "changed_" AND form[fields[i]]>
    			<cfset index = Replace(fields[i], "CHANGED_", "", "all")>
    			<cfset typeId = form["id_" & index]>
    			<cfset rate = form["rate_" & index]>
    			<cfset type = dao.getTaxRateTypeById(typeId)>
    </cfif>
    </cfloop
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    45
    Rep Power
    6
    Originally Posted by rawk
    I find ListToArray(form.FIELDNAMES) to be quite helpful in processing some of my more complicated forms. You can then loop over every form field that was submitted and handle it accordingly.
    ......
    Thanks very much, rawk. That's exactly what I did. I just modified the JS so that each iteration of the fields would have the iteration number in the element's name, parsed that out before running the query, and then updated the database. That way I was able to link each text field with the value selected in the ID drop-down box.

    The final 2 responses in this thread by kiteless and cfSearching also helped a great deal.

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo