I have a form processing that I am sure can be done more efficiently and there is an error in the result set although not "life threatening" just not correct.
The purpose of the page is to associate an item with a program and at the same time associate a designation within the program -- B
and F
fields are checkboxes allowing the item to be associated with multiple programs and designations within that specific program. (edited for clarity)
Example:
Item: Lightsaber
- Program: Jedi Training
- Designation: Tool(b) (yes)
Designation: Weapon(f) (no)
Program: Jedi Master
- Designation: Tool(b) (yes)
Designation: Weapon(f) (yes)
Program: Smuggler
- Designation: Tool(b) (no)
- Designation: Weapon(f) (no)
Form:
<form action="#CGI.SCRIPT_NAME#" method="post" name="program">
<label>Item</label>
<select id="item" name="item">
<!---//loop through and display items --->
<cfloop query="getitems">
<option value="#itemid#">#itemname#</option>
</cfloop>
</select>
<table>
<!---//loop through and display programs --->
<cfloop query="getprogram">
<tr>
<td>#programname#</td>
<td><input type="checkbox" id="B#programid#" name="B#programid#"></td>
<td><input type="checkbox" id="F#programid#" name="F#programid#"></td>
</tr>
</cfloop>
</table>
<input type="submit">
</form>
Action Page:
<!---// is there is a form being processed --->
<cfif #CGI.REQUEST_METHOD# is 'post'>
<!---// create program list from query --->
<cfset pl = ValueList(query.var, ','>
<!---// set addtl form var's --->
<cfset listassid = 'form.item'>
<!---// loop over program list --->
<cfloop list="#pl#" index="i">
<!---// loop over form fields --->
<cfloop list="form.fieldnames" index="field">
<cfif #field# EQ 'B'&#i#>
<!---// if field is B and var, set designation true --->
<cfset b = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset b = 'null'>
</cfif>
<cfif #field# EQ 'F'&#i#>
<!---// if field is F and var, set designation true --->
<cfset f = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset f = 'null'>
</cfif>
<cfif b EQ 'null' AND f EQ 'null'>
<!---// if both are null then skip --->
//do nothing
<cfelse>
<!---//insert record into table --->
insert into table (table fields)
(#i#, #listassid#, #b#, #f# )
</cfif>
</cfloop>
</cfloop>
</cfif>
The result should be:
id item_id program_id B F
1 24 1 x
2 32 2 x x
The actual result is:
id item_id program_id B F
1 24 1 x
2 32 2 x
3 32 2 x
Thank you in advance for any clarification and efficiencies you can suggest.