Reputation: 1
I am building an add/edit user form, when the page is accessed there is an if statement which detects if we are editing a current user or adding a new user by a url id
<cfif isDefined('URL.id')>
<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=#URL.id#
</cfquery>
<cfset #name#=#getSquadMember.athlete_name# />
<cfelse>
<cfset SESSION.squad = structNew()>
<cfparam name="SESSION.squad.name" default="">
</cfif>
That is fine but the problem comes in evaluting if the session or value exists, I get an error
<cfinput class="text" name="name" type="text" id="name" value ="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes" />
Element squad.name is undefined in session. Why is it evaluating if session exists when the first condition is met?
Thanks,
R.
Upvotes: 0
Views: 932
Reputation: 2438
The problem you are seeing is that IIF() has to evaluate all parts of the statement. You can't really use it for checking if a variable is defined or not as it will always try to evaluate the contents of both the true and false responses.
As long as you bear this in mind when using IIF() there's no problem with using it. Just keep it simple and don't try to use it when a variable might not exist.
I was going to point out the other issues with your code, but the other answers have already done a good job of covering these.
Upvotes: 0
Reputation: 1402
Couple of issues:
1)
<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=#URL.id#
</cfquery>
You're asking for a SQL injection here. Use cfqueryparam
2)
<cfset #name#=#getSquadMember.athlete_name# />
It's your second post where I notice weird use of #
It should be as Jason wrote it:
<cfset name = getSquadMember.athlete_name />
3) As for your question:
<cfinput class="text" name="name" type="text" id="name" value="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes" />
This way is not really readable. Set some variable for the value first. Second thing - you don't need to put the variables in DE like this, this will do:
DE(name)
And as you're using cfinput the whole thing probably needs to be correct. And another issue - you don't really need to use cfinput there. Normal HTML input will do and save you some CF parsing.
Upvotes: 0
Reputation: 561
First up - if you can possibly avoid using iif(), then do so. It causes many more headaches than it has ever solved...
However, note the following from the ColdFusion documentation on iif():
If a variable is undefined, ColdFusion throws an error when it processes this function. The following example shows this problem:
#IIf(IsDefined("Form.Deliver"), DE(Form.Deliver), DE("no"))# This returns "Error resolving parameter FORM.DELIVER".
To avoid this problem, use the DE and Evaluate functions in code such as the following:
#IIf(IsDefined("Form.Deliver"), Evaluate(DE("Form.Deliver")), DE("no"))# This returns "no"; ColdFusion does not throw an error.
Or, if you're on CF9 (which supports ternary operators):
<cfinput class="text" name="name" type="text" id="name" value ="#IsDefined('name') ? name : SESSION.squad.name#" required="yes" />
Upvotes: 3
Reputation: 1
Is there a reason as to why you need to have two different variable names for the same item? Could you just do:
<cfif isDefined('URL.id')>
<cfquery name="getSquadMember" datasource="#application.datasource#">
SELECT * from squad WHERE id=<cfqueryparam value="#URL.id#" cfsqltype="CF_SQL_INTEGER" />
</cfquery>
<cfset name= getSquadMember.athlete_name />
<cfelse>
<cfset squad = structNew()>
<cfset name = "" />
</cfif>
<input type="text" name="name" id="name=" value="#name#" />
Which would then in turn remove the need for the iff statement
HTH
J
Upvotes: 0