Skip to main content
September 5, 2008
Question

Create mutiple records w/dynamic checkboxes?

  • September 5, 2008
  • 4 replies
  • 2089 views
Greetings

This is no doubt an easy one, but can't seem to get multiple checkboxes displayed dynamically to create corresponding multiple records?

The form page has:

<form action="add_action.cfm" method="post">
<cfoutput query="get_all">
<tr bgcolor="###iif(currentrow MOD 2,DE('ffffff'),DE('d9ecff'))#">
<td class="tddynamic">#num#</td>
<td class="tddynamic">#descriptions#</td>
<td class="tddynamic"><INPUT Type="Checkbox" Name="ID"
Value="#thequery.ID#"</td>

Action Page?

Access, CF8

Thanks for your time in teaching an old dog new tricks...
This topic has been closed for replies.

4 replies

Inspiring
September 16, 2008
Change this
<cfset SubscriptionID = Evaluate('Subscription_#(BlogID)#')>

to this
<cfset SubscriptionID = form["Subscription_" & BlogID>

Better yet, read the entire thread. cfsearching has the best answer of anyone.

Finally, in case nobody mentioned it yet, you need to handle the scenario of a form submission with no checked boxes.
Inspiring
September 8, 2008
If the commodity codes are stored in another table, you can query that table and use a single SELECT to insert all of the records all at once, instead of using a loop.

It is a special type of INSERT that allows you to insert multiple records from another table using a SELECT. It is functionally equivalent to inserting the values one record at a time. However, it does not require multiple queries.

This hard coded example would insert five new records into your junction table for vendor_id 4. One for each of the five category_id's: 1,2,3,4,5

INSERT YourJunctionTable ( cccategory_ID vendor_ID )
SELECT cccategory_ID, 4
FROM YourCCategoryTable
WHERE cccategory_ID IN ( 1,2,3,4,5 )
Inspiring
September 5, 2008
Your checkbox is named id, not one_id. I'd do it like this

if form.id exists
cfquery
INSERT INTO junction_table
(one_ID, user_ID)
<cfloop list="#form.id#" index="idx">
select distinct
<cfqueryparam value="#idx#>
, <cfqueryparam value="#sesssion.user_id#>
from some_small_table
<cfif listgetat(form.id, idx) lt listlen(form.idx)>
union

closing tags

September 5, 2008
Yikes...

OK - for simplicity's sake, let's say that I am passing a product code (p_ID) and a buyer code (b_ID) for each checkbox, which will create record(s) in the junction table.

I'm confused about the construct- bear with me here...

Something like:

<cfif IsDefined("form.p_ID")>

<cfquery datasource="#Request.BaseDSN#">

INSERT INTO junction
VALUES(Form.p_ID, Form.b_ID)

<cfloop list="#form.p_ID#" index="idx">

SELECT DISTINCT <cfqueryparam value="#idx#">, <cfqueryparam value="#p_ID#">
FROM SOME OTHER TABLE?
<cfif listgetat(form.p_ID, idx) lt listlen(form.idx)>
union
</cfif></cfloop>
</cfquery>
</cfif>

?
Inspiring
September 5, 2008
newportri,

In general terms, what are the values being inserted into the junction table

- Multiple product codes for a single buyer
- One product code for multiple buyer's
- Multiple products and buyers

?
Inspiring
September 5, 2008
When you submit the form, you will either not get that form field at all or you will get a list. If you get a list, loop through it and do what you need to do.
September 5, 2008
Dan:

Thanks again and again...

The action page that would insert one record looks like:

<cfquery name="TheQuery" datasource="#Request.BaseDSN#">
INSERT INTO junction_table
(one_ID, user_ID)
VALUES (#Form.one_ID#, #Form.Session.user_ID#)
</cfquery>

How to loop through? Index loop?