Insert data from form with dynamically named fields

1.9k views Asked by At

OK. I've done a terrible job of explaining what I'm trying to do. I will try one more time to be more clear.

I have a list of variables that is submitted to a page with a cfquery insert. The variables come from dynamically named form fields, and are captured using a cfloop:

<cfloop list="#form.fieldnames#" index="item">

</cfloop> 

What I have are form fields dynamically named, and a value added, as such:

<input type="hidden" name="ticketid_#some_number#" value="#some_quantity#">

For brevity, lets say the form field name is ticketid_6, and the value is 4. This could be a different name and value (for instance, ticketid_3 with a value of 1), or there might be several form fields of similar construct with different names and/or values.

So, on the insert page, I need to insert ticketid_6 4 times (creating 4 separate rows) into my table. So, a row in the database for each dynamically named form field, times the value of each.

I hope that explains it better.

Leigh,

I am still open to your suggestion on the previous post, but I fear I might not have explained my situation clearly enough for you to give your best recommendation. I will re-approach that question after I get this part figured out.

3

There are 3 answers

4
Matt Busche On

If you need to loop through all your field names and treat each one of those fields as a list you'd need to do two loops to insert each item.

<cfloop list="#form.fieldnames#" index="item"><!--- loop through all the form fields --->
  <cfif find('ticketid_',item)><!--- if the form name contains 'ticketid_'. Use findNoCase if you want to ignore case sensitivity --->
    <cfloop from="1" to="#form[item]#" index="counter"><!--- loop through the number in form.ticketid_ --->
      <cfquery datasource="#dsn#">
      INSERT INTO table (fieldname, fieldValue, TicketNum)
      VALUES (
        <cfqueryparam value="#item#" cfsqltype="cf_sql_varchar">,--fieldName from the form
        <cfqueryparam value="#form[item]#" cfsqltype="cf_sql_varchar">--value of the fieldName in the form
        <cfqueryparam value="#counter#" cfsqltype="cf_sql_integer">--ticket number being inserted
       )
      </cfquery>
    </cfloop>
  </cfif>
</cfloop>

You'll need to do server side validation to verify they haven't entered a non numeric number for the input box, but assuming you've done that this should achieve what you're looking for.

6
James A Mohler On

You need to first bundle your data into an XML structure

<cfsavecontent variable="myFormData">
<cfoutput>
  <ul class="xoxo">
  <cfloop list="#form.fieldnames#" index="item">
    <cfloop list="#form[item]#" index="eachItem">
  <li><b>#xmlformat(item)#</b> <var>#xmlformat(eachItem)#</var>      
    </cfloop>
  </cfloop>
 </ul>
<cfoutput>
</cfsavecontent>

Then do a single insert

<cfquery>
   INSERT INTO table (formData)
   VALUES (<cfqueryparam value="#myFormData#" cfsqltype="cf_sql_varchar">)
</cfquery>

When you pull the data, you can

  • Show it as is, a bulleted list,
  • Promote to a bunch of rows in a table

Note that the data is inserted as both HTML and XML

1
Azam Alvi On

You need to use Evaluate function to get form dynamically input field value Try this one

<input type="hidden" name="ticketid_#some_number#" value="#some_quantity#">

<cfloop list="#form.fieldnames#" index="item">
   <cfoutpuy>Evaluate("ticketid_#item#")</cfoutpuy>
</cfloop>