Skip to main content
August 12, 2011
Question

Multiple Checkbox Insert Handling

  • August 12, 2011
  • 1 reply
  • 1986 views

Every so often, I need to refresh my knowledge regarding how to handle multiple check box selections, and how they get handled on an action page to insert correctly in a DB.

In a helpdesk application, the admin assigns projects to technicians. He may assign one project to several technicians.

The first action page inserts the data from the form into the DB with time and date stamps, due dates, etc.

Here is where the selected checkboxes do not get passed.

The second action page depends on the fist, populating a junction table using:

<cfquery name="get_project_ID" datasource="#Request.BaseDSN#">
   SELECT MAX (project_ID) as lastID
   FROM main_projects
</cfquery>

    <cfloop index="i" list="#tech_ID#">
        <cfquery name="TheQuery" datasource="#Request.BaseDSN#">
            INSERT
            INTO     junction_project_tech
                    (junc_tech_ID, junc_project_ID)
            VALUES     (
                        <cfqueryparam cfsqltype="cf_sql_integer" value="#i#">,
                        #get_project_ID.lastID#
                    )
        </cfquery>
    </cfloop>

Any help would be greatly appriciated-


newportweb

This topic has been closed for replies.

1 reply

Inspiring
August 12, 2011

T

The second action page

How may <form>'s are involved?

Here is where the selected checkboxes do not get passed.

What do you mean by "do not get passed"? It would help to see your form code, or at least the section dealing with checkboxes.

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

   SELECT MAX (project_ID) as lastID
   FROM main_projects
</cfquery>

That is not a thread safe way to retrieve a newly inserted record. What is your database type?

August 12, 2011

Thanks for your quick reponse. I hope you don't cringe easily...

1 form:

<cfquery name="get_tech" datasource="#Request.BaseDSN#">
SELECT *
FROM lookup_tech

</cfquery>   
<form action="project_action.cfm" method="post">

<p><strong>Description:</strong></p>
<p><textarea cols="120" rows="10" name="project_desc" wrap="hard" class="inputtext"></textarea></p>


<p><strong>Assigned Technicians</strong></p>
<cfoutput query="get_tech">
    <tr>
       
<td width="75%" class="tddynamic">#tech_lname# <div class="sbfield"><strong>Due Date:</strong> <input type="text" name="due_date" size="8" class="sbfield"> </td>
<td width="25%" class="tddynamic"><INPUT Type="Checkbox" Name="tech_ID" Value="#tech_ID#"</td>
</tr>
</cfoutput>

<cfoutput>
<input type="hidden" name="assign_date" value="#DateFormat(CreateODBCDate(Now()), "mm/dd/yyyy")#">
<input type="hidden" name="assign_time" value="#TimeFormat(CreateODBCTime(Now()), "hh:mm tt")#"></cfoutput>

<input type="submit" name="" value="Submit Project" class="formbutton">

</td>
</tr></form>

Action1

<CFPARAM Name="tech_ID" Default=0>
<cfquery name="create_project" datasource="#Request.BaseDSN#">

INSERT INTO main_projects (

project_desc,

assigned,

assign_date,

assign_time,

due_date,

assigned_tech1,

assigned_tech2,

assigned_tech3,

assigned_tech4,

tech_1_hours,

tech_2_hours,

tech_3_hours,

tech_4_hours


)

VALUES  (

'#project_desc#',

1,

'#Form.assign_date#',

'#Form.assign_time#',

'#FORM.due_date#',

(insert checkbox values from form here),

0,

0,

0,

0

)</cfquery>

<cflocation url="project_action2.cfm?tech_ID=#Form.tech_ID#" addtoken="No">

Action2: You have seen.

DB: MS Access- I know, I know. I'm getting to SQL Server soon.

Thanks again....

Inspiring
August 12, 2011

I do not know if you have control over the structure, but a few questions first :-)

1) Why two action pages? It seems like both inserts should happen as a single unit.  By seperating them you create the possibility of data problems. For example what would your application do if the first step completed but the second one failed?

2) If you already have a junction table, what's the purpose of these columns?

       assigned_tech1,

       assigned_tech2,

       assigned_tech3,

      assigned_tech4,