[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Checkboxes are checked when they should not be ...

I have a form that displays the existing values in a database.
Two of the fields are checkboxes. The values in the database are "0" or "1" (0 for false or no, 1 for true or yes).
I'm displaying the fields in a repeat region (see code below).
All of the checkboxes that SHOULD be checked are checked ...BUT ... so are many others that should not be checked.
Values are displayed in a table with a column for IngrName, a column for Pantry (checkbox), and column for StockItem (checkbox).
In the display of the data, everything up to & including the last record for which Pantry=true is checked displays a checkmark in the box. Ditto for the StockItem checkbox.
I have tested the recordset and the values are indeed what I think they are ... they are not all checked as the display would lead you to believe.
IngrID is the primary key.
For the IIF clauses, I've tried comparing to "1" and "true" with the exact same results.
I've successfully displayed checkbox fields using code like this before; the only difference was those were all strung out on one line and there was a separate field in the database for each one.
So this must have something to do with using the repeat region and a record set but I am at a loss.
What the heck is going on?
Here's my code:

<cfoutput query="rs_Ingredients">
  <tr><td width="200"><p>
   <input type="checkbox" name="item_#rs_Ingredients.IngrID#" />
  #rs_Ingredients.IngrName#</p></td>
  <td width="50">
  <input name="pantry_#rs_Ingredients.IngrID#" type="checkbox"
  <cfoutput>#IIF(rs_Ingredients.Pantry eq "yes",DE("checked"),DE(""))#</cfoutput>/>
  </td><td width="50">
  <input name="stock_#rs_Ingredients.IngrID#" type="checkbox"
  <cfoutput>#IIF(rs_Ingredients.StockItem eq "yes",DE("checked"),DE(""))#</cfoutput>/>
</cfoutput>
0
alicia1234
Asked:
alicia1234
  • 7
  • 3
  • 2
1 Solution
 
alicia1234Author Commented:
The recordset is defined as this:
<cfquery name="rs_Ingredients" datasource="connCWArecipes">
   SELECT IngrID, IngrName, Pantry, StockItem
   FROM tblIngredients
   ORDER BY IngrName
</cfquery>
0
 
alicia1234Author Commented:
By everything being checked up to the "last", I mean like this (using Pantry as an example):

The Pantry field for all of these items is displayed as checked:
Artichokes
Canned diced tomatoes
Leeks
Olive Oil

The Pantry field for these items is NOT displayed as checked:
Pepper
Red Peppers
Tomatoes

In the database, only "Canned diced tomatoes" and "Olive Oil" have the Pantry field set to true.
0
 
PluckaCommented:
alicia1234,

Try this

<cfoutput query="rs_Ingredients">
    <tr><td width="200"><p>
    <input type="checkbox" name="item_#IngrID#" />
    #IngrName#</p></td>
    <td width="50">
    <input name="pantry_#IngrID#" type="checkbox"
    <cfoutput>#IIF(Pantry eq 1,DE("checked"),DE(""))#</cfoutput>/>
    </td><td width="50">
    <input name="stock_#IngrID#" type="checkbox"
    <cfoutput>#IIF(StockItem eq 1,DE("checked"),DE(""))#</cfoutput>/>
</cfoutput>

Regards
Plucka
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
alicia1234Author Commented:
That doesn't change anything.
I wouldn't think it would ... you've just taken out the explicit reference to rs_Ingredients, right?
0
 
PluckaCommented:
yes and changed 'yes' to 1 in the iif's

Perhaps your logic is wrong.

Are you sure

    <input name="pantry_#IngrID#" type="checkbox"
    <cfoutput>#IIF(Pantry eq 1,DE("checked"),DE(""))#</cfoutput>/>

shouldn't be something like

    <input name="pantry_#IngrID#" type="checkbox"
    <cfoutput>#IIF(IngrID eq 1,DE("checked"),DE(""))#</cfoutput>/>

I don't understand your tables enough, but those field names don't seem to match up.
0
 
alicia1234Author Commented:
No, definitely not IngrID ...

There is only one table: tblIngredients:

IngrID AutoNumber (primary key)
IngrName Text
Pantry Yes/No
StockItem Yes/No



0
 
bwasyliukCommented:
Can you try this code:

<cfoutput query="rs_Ingredients">
    <tr><td width="200"><p>
    <input type="checkbox" name="item_#IngrID#" />
    #IngrName#</p></td>
    <td width="50">#Pantry#<input name="pantry_#IngrID#" type="checkbox" #IIF(Pantry eq 1,DE("checked"),DE(""))# />
    </td><td width="50">#StockItem#<input name="stock_#IngrID#" type="checkbox" #IIF(StockItem eq 1,DE("checked"),DE(""))# />
</cfoutput>

This will put the value next to each checkbox so you can validate exactly what ColdFusion thinks those variables are as it goes through the loop.

Also, as a practice, I always fully qualify my variable names, just in case they are defined farther up in the page and may have the wrong values.

So, in your code, I would change #IngrName# to #rs_Ingredients.IngrName# and so on...
0
 
alicia1234Author Commented:
byasyliuk: My code does have the fully-qualified names (see originial post) ... it was Plucka's that didn't. ;-)
I did what you suggested about displaying the values ...
you didn't quite have the variables correct so I've pasted in my current code here to make it easier to see where I'm at. The values display correctly. That is to say, I do see "0" for when the box should not be checked, and "1" for when it should. But I see "0" next to a checked box! And sometimes "0" next to a not-checked box. "1" is always next to a checked box. Read my comment above about how everything seems to be checked up to and including the last "checked" item in my database (alphabetically).

Code as it now stands:

<cfoutput query="rs_Ingredients">
<tr class="smalltext">
<td width="200"><p><input type="checkbox" name="item_#rs_Ingredients.IngrID#" />
#rs_Ingredients.IngrName#</p>      </td>
<td width="50">#rs_Ingredients.Pantry#
<input name="pantry_#rs_Ingredients.IngrID#" type="checkbox"
<cfoutput>#IIF(rs_Ingredients.Pantry eq "1", DE("checked"),DE(""))#</cfoutput>/>
</td>
<td width="50">#rs_Ingredients.StockItem#
<input name="stock_#rs_Ingredients.IngrID#"type="checkbox"
<cfoutput>#IIF(rs_Ingredients.StockItem eq "1", DE("checked"),DE(""))# </cfoutput>/>
</cfoutput>
0
 
bwasyliukCommented:
Sorry, my bad. :)  

Now, it shouldnt make any difference, but can you remove the extra <cfoutput>s that you have around your #IIF()#.  Its extra code that shouldnt be there, and I am curious if it will change anything.

0
 
alicia1234Author Commented:
That did it!!!!  Unbelievable! THANK YOU!!!!!
Solution was simply to remove the <cfoutput> tags that were within the <input> tags.
Final solution:
<cfoutput query="rs_Ingredients">
<tr>
<td width="200"><p><input type="checkbox" name="item_#rs_Ingredients.IngrID#" />
#rs_Ingredients.IngrName#</p>     </td>
<td width="50">
<input name="pantry_#rs_Ingredients.IngrID#" type="checkbox"
#IIF(rs_Ingredients.Pantry eq "1", DE("checked"),DE(""))#/>
</td>
<td width="50">
<input name="stock_#rs_Ingredients.IngrID#"type="checkbox"
#IIF(rs_Ingredients.StockItem eq "1", DE("checked"),DE(""))#/>
</cfoutput>
0
 
alicia1234Author Commented:
So was the problem that I had <cfoutput> tags embedded within a <cfoutput> tag?
0
 
bwasyliukCommented:
Yes, that appears to be the case.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now