Reply to topic
Accessing a checkbox value via CF
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 770
Location: Newark, De
Reply with quote
Having a CF coding problem, so I thought I might post it here for our developers and clients. Basically, I need to access the value of a checkbox using a CFif statement in order to make a dynamic SQL query.

So, the checkbox looks like this:

Code:
<form name="form1" method="post" action="">
<input type="checkbox" name="color" value="white">White

...blah blah

</form>


and the query is as so:
Code:

<cfquery name="showmyCards" datasource="#dsn_name#" username="#dsn_login#"
password="#dsn_password#">

select mycards.userID, mycards.CardID, users.username, tblcards.CardTitle
from mycards, users, tblcards
where mycards.userID = users.userID
and mycards.CardID = tblcards.CardID
<cfif color = white>
and tblcards.ColorCode = "W"
</cfif>
ORDER BY users.username
</cfquery>

<cfoutput query="showmyCards">
<li>#username#, #CardTitle#</li>
</cfoutput>


Now, the problem block of code is

Code:
<cfif color = white>
and tblcards.ColorCode = "W"
</cfif>

If you comment that out, the rest works. Alternatively, I've tried to do this using

Code:
<!--- set variables --->
<cfif color1.value EQ true>
<cfset color1 = "White">
<cfelse>
<cfset color1 = "">
</cfif>


and then

Code:
<cfquery name="showmyCards" datasource="#dsn_name#" username="#dsn_login#" password="#dsn_password#">
SELECT mycards.userID, mycards.CardID, users.username, tblcards.CardTitle
FROM mycards, users, tblcards
WHERE mycards.userID = <cfqueryparam value="#users.userID#" cfsqltype="cf_sql_integer">
AND mycards.CardID = <cfqueryparam value="#tblcards.CardID#" cfsqltype="cf_sql_integer">
AND mycards.ColorCode =
<cfif color1 NEQ "">
<cfqueryparam value="#color1#" cfsqltype="cf_sql_char">
AND mycards.ColorCode =
</cfif>


for my query, but that errors on the 'set variables' section with

Element VALUE is undefined in COLOR1.
steve
HostMySite Developer

Joined: 02 Mar 2004
Posts: 30
Reply with quote
Jamie,

The only time a the Form.color variable will exist is if it is checked.
So you first need to be sure that variable exists. Also, in Coldfusion you would have to use EQ intstead of the = sign when comparing to values. The code would look something like this:

Code:

<cfif isdefined("form.color") and trim(form.color) EQ "white">
and tblcards.ColorCode = "W"
</cfif>
bobclingan
Forum Regular

Joined: 16 Sep 2004
Posts: 271
Location: Abingdon, MD
Reply with quote
or use cfparam

Code:
<cfparam name="form.yourVariable" default="yourValue">
Accessing a checkbox value via CF
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic