Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cfif - trying to get a checkbox to display an SQL bit field 1

Status
Not open for further replies.

nerdcore

MIS
Jan 23, 2003
26
0
0
I don't know exactly which forum to post this in but since I'm using coldfusion to build this app I'm trying here first.

I have an MS SQL Server 2000 backend. I am developing a front end using coldfusion. I am using normal HTML input tags rather than CF tags for the fields. I am BRAND NEW to cold fusion so bear with me.

I have a form which displays a contact record for a person. This record contains typical name, address, phone, etc. and then a boolean field for "Same_Address" which tells me whether this contact has the same address as the company to which he belongs. This Same_Address field is of type 'bit' in the SQL Server and has a default value there of (0).

But when I add an <input type=&quot;checkbox&quot; value=&quot;#Same_Address#&quot;> to my page it does not show up. It is always blank. So I tried using a cfif statement to populate it which works except I would think that there is an easier way to do this:

<cfif #Same_Address# eq '1'>
<td><input type=&quot;checkbox&quot; checked name=&quot;Same_Address&quot; value=&quot;#Same_Address#&quot;></td>
<cfelse>
<td><input type=&quot;checkbox&quot; name=&quot;Same_Address&quot; value=&quot;#Same_Address#&quot;></td>
</cfif>

I ask because my next project is to make another page that has about 100 checkboxes on it and would hate to write 100 cfif statements to do this. Also in the SQL server the actual values in the field are 0 and -1 while they appear to be 0 and 1 in ColdFusion.

I also would like to know where i would put script code that would check the value of the Same_Address checkbox and disable editing of the address fields if it was checked (and likewise enable those fields if Same_Address was not checked). I can put code in the onClick even of the checkbox but that does not run on 'startup' of the form and putting it in the onLoad event of the <body> tag doesn't work either.

Thanks in advance, and help is greatly appreciated!
 
As far as I know, that's the only way, using pure CFML, to check or not check a checkbox based on a given value, unfortunately. Since the value of a checkbox has nothing to do with whether it's checked or not.

There are a couple of options, though. Write out your checkbox values into a javascript array, and then have a javascript function check all the appropriate boxes in an onLoad of the page.

For 100 checkboxes, though, even that could get tedious.
One nice dynamic way to do it, though, might work for you.
Build a ColdFusion list that holds all the names of your checkboxes. Then you can loop through that list and check or not check the box as appropriate. This way, you're only coding a single checkbox (so it's easy to maintain if you want to change something about how they look).
Code:
<CFSET lstCheckboxnames = &quot;cb1,cb2,cb3,cb4...&quot;>

<CFLOOP list=&quot;#lstCheckboxnames#&quot; index=&quot;whichCheckbox&quot;>
   <CFSET sCheckboxFlag = &quot;&quot;>
   <CFIF qryName[whichCheckbox]>
       <CFSET sCheckboxFlag = &quot;checked&quot;>
   </CFIF>
   <CFOUTPUT><td><input type=&quot;checkbox&quot; #sCheckboxFlag# name=&quot;#whichCheckbox#&quot; value=&quot;#qryName[whichCheckbox]#&quot;></td></CFOUTPUT>
</CFLOOP>
That single loop will produce however many checkboxes are named in lstCheckboxnames.

Any non-zero value evaluates to true in ColdFusion. So both -1 and 1 will check the box, while a zero will not.


-Carl
 
I never had very much success with check boxes in ColdFusion and believe me I tried. So, I just opt to use select boxes. I feel they give you much better control.
 
A checkbox is a completely valid interface element... and the accepted one to use for on/off situations.

I'm not sure what you mean by a select box &quot;giving you more control&quot;... if it's a true/false question, how much more control do you need?

While it's true that CFPARAMing the value of a checkbox variable in the action page is generally a good idea, one line of code is a small price to pay in order to present the user with the interface that they're used to or would expect to see.

I've never had any problem using checkboxes whatsoever.

Even if you used selectboxes, you'd still need to loop through each option and determine if it's the one that's supposed to be selected... so I'm not sure how that saves you anything.


-Carl
 
thanks for the input guys....i am going to use the checkboxes because i feel it is a bit cleaner and easier to run down a list of 100 items and just check them off rather than drop down/select a yes/no.

I am cfparam-ing all my checkboxes on the submit page so i can capture any blank (false) checkboxes.

I still have one question which I kind-of solved but am thinking there might be a cleaner way to do it. Upon loading a contact record, i want to check if one of the boolean fields (the Same_Address checkbox) is true or false. If it is true I want to disable all the address fields for the contact, as this contact is flagged as having the same address as the main company to which he belongs, and there's no need to edit those fields. If the Same_Address field is false then the address fields will be enabled and accept input. Currently i have a CFIF statement in each INPUT tag which checks the Same_Address field and disables the control if necessary. My question: Is there somewhere I could write ONE line of script to disable these controls based on the Same_Address value? I tried the OnLoad event for the <body> but that doesn't work, (it appears to run before cold fusion has processed the records?). Is there some tag that will allow me to run a script function (Address1.disabled=true;Address2.disabled=true) after the page has loaded? I have like 10 fields i need to disable/enable. I have these 10 disable/enable when clicking the Same_Address checkbox, but i want to also have this happen initially on page load and would like to control it from ONE place instead of a CFIF in every <input> tag.
 
Can you not run the same script that you run when clicking the checkbox on the onload? Or does the script only disable (no checking of the checkbox state)
Otherwise you can run the function to check the state when you load the page and set the fields enabled or disabled. You can also do a cfif to test the data and set the address to enabled or disabled, this runs before the page is created.

Erwin Oosterhoorn
Analyst Programmer,
ice hockey player/fan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top