Coldfusion #iif 和会话
我正在构建一个添加/编辑用户表单,当访问页面时,有一个 if 语句检测我们是否正在编辑当前用户或通过 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>
这很好,但问题在于评估会话是否或者值存在,我收到错误
<cfinput class="text" name="name" type="text" id="name" value ="#IIf(IsDefined('name'), DE('#name#'), DE("#SESSION.squad.name#"))#" required="yes" />
Elementquad.name 在会话中未定义。为什么在满足第一个条件时评估会话是否存在?
谢谢,
R。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先 - 如果您可以避免使用 iif(),那么就这样做。它引起的麻烦比它解决的问题还要多……
但是,请注意 ColdFusion 文档中有关 iif() 的以下内容:
或者,如果您使用的是 CF9(支持三元运算符):
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():
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" />
为什么同一个项目需要两个不同的变量名称,是否有原因?你能这样做吗:
这将反过来消除对 iff 语句
HTH
J的需要
Is there a reason as to why you need to have two different variable names for the same item? Could you just do:
Which would then in turn remove the need for the iff statement
HTH
J
有几个问题:
1)
您在这里请求 SQL 注入。使用 cfqueryparam
2)
奇怪
这是你的第二篇文章,我注意到 #它应该像 Jason 写的那样
: 3)至于你的问题:
这种方式并不是真正可读。
首先为该值设置一些变量。
第二件事 - 您不需要像这样将变量放入 DE 中,这样就可以了:
并且当您使用 cfinput 时,整个事情可能需要正确。还有另一个问题 - 你真的不需要在那里使用 cfinput 。普通的 HTML 输入就可以了,并且可以为您节省一些 CF 解析。
Couple of issues:
1)
You're asking for a SQL injection here. Use cfqueryparam
2)
It's your second post where I notice weird use of #
It should be as Jason wrote it:
3) As for your question:
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:
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.
您看到的问题是 IIF() 必须评估语句的所有部分。
您不能真正使用它来检查变量是否已定义,因为它总是尝试评估 true 和 false 响应的内容。
只要在使用 IIF() 时牢记这一点,使用它就没有问题。只要保持简单,当变量可能不存在时不要尝试使用它。
我本来打算指出您的代码的其他问题,但其他答案已经很好地涵盖了这些问题。
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.