Link to home
Start Free TrialLog in
Avatar of Eric Bourland
Eric BourlandFlag for United States of America

asked on

Checkbox will not stay checked on edit page

ColdFusion 9
MS SQL Server 2005

I feel like I am missing something obvious. I've built a simple form that creates / updates a database record.

The edit page contains a checkbox: Archive.

When I update a record and check the Archive checkbox, the database table is updated; column "Archive" is set to "True" for the edited record. That works as expected.

When I view the record again, the checkbox is unchecked. However the column "Archive" in the data table is still, of course, set to True.

I am following good, working examples set for me previously by _agx_, gdemaria, and others who have helped me with my numerous ColdFusion form questions.

What am I missing? Why does the Archive checkbox display as unchecked on the edit page, when its value is "True" in the Archive data column?

Thank you as always for any advice.

Eric
QUERY:

<cfif isDefined('form.Archive')>
<!--- checkbox has been checked and is present in post data --->
<cfset form.Archive = 1>
<cfelse>
<cfset form.Archive = 0>
<!--- checkbox not checked and not present --->
</cfif>

<cfquery datasource="#application.datasource#" name="qry_editPage">
SELECT projectID
,ProjectName
,OSMVISTA
,Supervisor
,Address
,City
,State
,ZIP
,County
,Telephone
,Email
,Website
,CongressionalDistrict
,ProjectDescription
,DateCreated
,DateModified
,Archive
FROM whwtProjects
WHERE projectID = <cfqueryparam value="#val(url.projectID)#" cfsqltype="cf_sql_integer">
</cfquery>


HTML:
<strong>Archive Project:</strong> <cfinput type="checkbox" name="Archive" value="1" checked="#Archive eq 1#" />

Open in new window

Avatar of Brijesh Chauhan
Brijesh Chauhan
Flag of India image

Try this...

HTML
<strong>Archive Project:</strong> <input type="checkbox" name="Archive" value="1"  <cfif Archive EQ 1>checked="checked"</cfif> />

Open in new window

SOLUTION
Avatar of Brijesh Chauhan
Brijesh Chauhan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing changes in your code.. just change the CFINPUT to have this


<cfinput type="checkbox" name="Archive" value="1" checked="#IIF(Archive IS 1,1,0)#" />

Open in new window

, this would do the work ...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eric Bourland

ASKER

Hmm. I tried many of the above ideas on my own last nignt, though I see how I made small errors.

I am going to work on this task and get back to you all later today .... thank you for these very useful ideas.

Eric
I'm going to address these suggestions one by one. I have read all responses carefully.

brij -- last night, till late, I was trying ideas along this line:

<cfinput type="checkbox" name="Archive" value="1" <cfif Archive EQ 1>checked="checked"</cfif> />

but when I try to put a CFIF statement inside the CFINPUT tag I always get some kind of error; in this case it is:

Invalid token c found on line 379 at column 68. 

 The error occurred in C:/websites/www.hardrockteam.org/admin/editProjects.cfm: line 379

377 : <cfinput type="text" size="50" name="CongressionalDistrict" value="#form.CongressionalDistrict#" tabindex="12" /></td></tr>
378 :              	<tr><td><strong>Archive Project:</strong> 
379 :                 <cfinput type="checkbox" name="Archive" value="1" <cfif Archive EQ 1>checked="checked"</cfif> />
 

Open in new window


Next, I tried the next solution that you suggested; this omitted the error, but every time I loaded the edit page, for any record, I saw that the Archive checkbox was selected by default.

I bet brij's solution would work, with some troubleshooting -- I am sure that I am missing something obvious and useful in Brij's code or ideas.

*****

OP_Zaharin: actually, ColdFusion populates the Archive column with values True or False, rather than 1 or 0. (This has always bugged me; I know there are workarounds for this.)

I did scope the variable Archive in query qry_editPage ... I tried a lot of variable scoping till late last night (gdemaria makes a good point, or course, about scoping variables).

However, your suggestion:

<cfinput type="checkbox" name="Archive" value="1" checked="#qry_editPage.Archive eq 1#" />


works perfectly. gdemaria suggested this solution too.

The frustrating part is, I coulda sworn I tried <cfinput type="checkbox" name="Archive" value="1" checked="#qry_editPage.Archive eq 1#" />

late last night, around 2 a.m.  ... obviously I messed it up, somewhere, because this works:

<cfinput type="checkbox" name="Archive" value="1" checked="#qry_editPage.Archive eq 1#" />

and what I did last night did not. What I get for working while very tired.

For purposes of education I am going to try some other of gdemaria's ideas -- they look interesting.

I will be back later to close this question. Thank you to all of you.

Eric

Eric, note that if this works...

  checked="#qry_editPage.Archive eq 1#" />


Then the value of archive is being tested again "1"... not "true"or "false"  

So, you may not be correct about how the value is being stored in the database.    If the data type is "bit" then it is storing 1 and 0, but when you view the database, it could display as true/false just for display purposes

the val() function will handle any numeric or empty value as 0 = false and 1 or more = true.   That's why it's handy
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also use <cfinput type="checkbox" name="Archive" value="1" checked="#IIF(Archive IS 1,1,0)#" />, IFF is a ternary operator, is archive is 1, it sets checked = 1 otherwise set it to 0...
>>So, you may not be correct about how the value is being stored in the database.    If the data type is "bit" then it is storing 1 and 0, but when you view the database, it could display as true/false just for display purposes

You are right. Datatype in column Archive is "bit". That means 1 or 0; thus, True and False are for display purposes. (I'd rather SQL Server show me the 1 and 0.)

>>the val() function will handle any numeric or empty value as 0 = false and 1 or more = true.   That's why it's handy
 
I heart the val() function.

Thanks for this.
brij -- got it. Thank you for that clarification. I am looking again at your solution. E
as of @gd post , >> I would not use IIF() though just because of the performance hit.

read this ..

http://www.bennadel.com/blog/162-ColdFusion-IIF-vs-CFIF.htm
I actually read that Ben Nadel post several months ago when I was researching another CFIF problem. Nadel's decidely un-PC blog posts ("bodaciously...") always crack me up (and make me look over my shoulder to make sure my gf does not see me reading them). I've learned a lot from reading his posts, though. The discussion that follows this blog post is illuminating -- at least for me.

I tend to stick to CFIF because that is what I have learned and practiced over a few years. I do want to learn more about using IIF in form fields.

@brijesh,  Ben Nadel is a very smart guy, I could not disagree with his findings although there is plenty of documentation showing the IIF() and Evaluate() are both less efficient.    Apparently, not in practice.   My second argument against it would be readability, particularly once you start adding the commonly needed de() functions to it.

In this case, I would argue against it because, it just over kill.   Why do you need an if statement to see if x = 1 then 1 else 0 ?   If x is populated, then you on need X  (checked="#x#")   if checked may be empty, just wrap it in val()...

> Nadel's decidely un-PC blog posts ("bodaciously...")

Lol, I haven't noticed anything like that...   for example?
>> discussion that follows this blog post is illuminating

It is a good a discussion. Personally I've never cared whether IIF causes a 2ms performance hit or not (evaluate is a different story) I just think the IIF/DE syntax ugly ;) (Sorry guys) It's always reminded me of MS Access, which used IIF(..) instead of supporting CASE like a real database.  But on the flip side, I like the new ternary operator. It's slick and intuitive. But some people *hate* it ... so go figure ;)

IMO it's about personal preference.
>> Lol, I haven't noticed anything like that...  

Lol.. I hope you're joking. Otherwise you must have fallen asleep on your keyboard to not notice ;)
The solution that is working is:

<cfparam name="form.archive" default="0"> (this is much more tidy than what I had before)

<cfinput type="checkbox" name="Archive" value="1" checked="#val(qry_editPage.Archive)#" />

(I used the val() function to ensure that the value for checked is an integer.)

Brij, gdemaria, and OP_Zaharin all contributed to this solution, so if it's cool, I will distribute points equally. Thank you _agx_ for your comments too.

It's working and the client is pleased. =) Thanks as always.

Eric
This worked like a charm. As usual, I learned something.
>> But on the flip side, I like the new ternary operator. It's slick and intuitive.

hmmm.. so CF 9 replaces IFF() with a new ternary operator..

<cfset checked = (archive IS 1)? 1 : 0 />

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09d55-7ffc.html#WSc3ff6d0ea77859461172e0811cbec09d55-7ff7
 
Well I wouldn't use it for boolean tests, because they're clear enough already. But there's actually more differences

>> so CF 9 replaces IFF() with a new ternary operator..
Well I wouldn't use it for boolean tests. But yeah, there's actually a difference between the two.  My favorite line from another Ben Nadel article sums it up well ;-)
 
"With the ternary operator, however, we finally get IIF() functionality without the skeezy feeling of delayed evaluation because the ternary operator is truly conditional execution...."