3 Replies Latest reply on May 17, 2009 5:22 AM by Dan Bracuk

    how to add dynamic jquery fields into DB

    davella

      I am using the script from: http://www.coldfusionjedi.com/index.cfm/2009/2/19/Using-jQuery-to-add-form-fields to dynamically add fields to a form.  I am having trouble actually inserting the fields into a DB.  It seems that the field name is the same each time so they just get combined into one variable.  For instance, if field 1 is "John" and they add another person and his name is "Frank" field1 becomes, "John, Frank".

       

      I have a few more fields then that, so I am unsure how to proceed.  Anyone have any ideas or code samples that have worked for them?  Essentially I am trying to loop through the form and insert each additional field seperately.

       

      Any ideas?

       

      Thanks a ton

        • 1. Re: how to add dynamic jquery fields into DB
          Dan Bracuk Level 5

          Nested loops.  Outer loop is your formfields.  Inner loop is each field (treat them as lists).

          • 2. Re: how to add dynamic jquery fields into DB
            davella Level 1

            Thanks for the help!  I am still stuck as to exactly how this will be done. My form is as follows:

             

            Type:

            Price:

            Unite:

            Name:

             

            and users can add another product etc so there will be more fields for each additional product (which all seem to get truncated as a comma seperated list in the form value)

             

             

            Here is what I have so far, after the form is submitted:

             

            <cfloop item="key" collection="#form#">

             

            <cfquery name="insertnote">
            INSERT INTO Deals (type, price, unit, name) VALUES ('#form.type#', #form.price#, '#form.unit#', '#form.name#'  )
              </cfquery>
             
            </cfloop>

             

            I honestly don't use loops like this very often so I apologize for having a lack of understanding.  I know you mentioned making them nested loops but I dont' know how to do it and have it successfully insert the appropriate data.  Everytime I try it doesnt come out right.

            • 3. Re: how to add dynamic jquery fields into DB
              Dan Bracuk Level 5

              Hopefully, each time the user adds a product, something is appended to the end of every form field.  It becomes very easy.  In fact you don't even need nested loops.

               

              <cfloop from = "1" to = listlen(form.name) index = "i">

               

              <cfquery name="insertnote">
              INSERT INTO Deals

              (type, price, unit, name)

              VALUES

              ('#listgetat(form.type, i)#', #listgetat(form.price, i)#, '#listgetat(form.unit, i)#', '#listgetat(form.name, i)#'  )
              </cfquery>

               

              Also, cfqueryparam for a number of reasons.