2017-09-13 95 views
0

当用户单击表中特定行的编辑时,我希望在同一页面中的输入文本框填充该行的详细信息。我试过这个,但没有奏效。如何将查询中的值设置为输入文本框

<cfquery name="getDataForEdit" datasource="dsn"> 
    select * from usercardetails where id = '#url.id#' 
</cfquery> 
<cfoutput> 
    <cfset #form.username# = #getDataForEdit.username#> 

</cfoutput> 
+0

您的谷歌搜索字符串是'coldfusion相关的表单字段'。 –

回答

2

尝试这个作为一个例子,让你去....

<cfquery name="getDataForEdit" datasource="dsn"> 
    select * from usercardetails where id = '#url.id#' 
</cfquery> 

<!--- 
OPTIONAL IMPROVEMENT: You might change your SQL to use CFQueryParam, this will protect against SQL injection 
select * from usercardetails where id = <cfqueryparam cfsqltype="CF_SQL_NUMERIC" value="#url.id#" /> 
---> 

<!--- 
OPTIONAL FOR DEBUGGING: If you uncomment this block it will show you the results of your query. 
<cfoutput><cfdump var="#getDataForEdit#" label="getDataForEdit" expand="No" /><br /></cfoutput> 
---> 

<cfoutput> 

<cfif getDataForEdit.recordcount is 1> <!--- Check that you only get one row back in results ---> 

    <!--- Coldfusion will build forms for you using cfform, but many coders keep away from it, so instead you need to build the HTML of your form yourself. ---> 

    <form action="index.cfm" method="post"> 
     <p><label for="username">Username</label><input type="text" id="username" name="username" value="#getDataForEdit.username#" /></p> 
     <p><input type="submit" id="butty01" name="butty01" value="Go" /></p> 
    </form> 

<cfelseif getDataForEdit.recordcount is 0> <!--- If no results ---> 
    <p>No records found</p> 
<cfelse> <!--- Anything else will mean many results ---> 
    <p>An error occurred (Multiple records with this ID found)</p> 
</cfif> 

</cfoutput> 

我已经把意见在周围的一些可选的改进的代码。

还要注意的是...

<cfset #form.username# = #getDataForEdit.username#> 

会导致您的问题。

<cfset username = getDataForEdit.username> 

将创建/设置一个变量用户名等于您的查询中的用户名列的值。在这种情况下,#标签不是必需的。 #打印出一个变量的值,这样你就可以这样做......

<cfset username = "#getDataForEdit.username#"> 

这将打印的用户名列的值转换成文本串(唯一的字符串作为值) ,并且文本字符串将被分配给变量用户名。

form 

是保留字,因为它是一个结构(这是值得仰视结构),它包含所有张贴到你的页面的表单变量数据。您可以通过使用cfdump

<cfoutput><cfdump var="#form#" label="form" expand="No" /><br /></cfoutput> 

要打印出表格内的一个值检查形式,你可以做

#form.username# 

左右(而且很容易混淆,当你是一个初学者)...

<cfset #form.username# = #getDataForEdit.username#> 

将创建一个名称与表单字段username的值对应的变量,并将其填入您的查询中的用户名。你真的不想那样。

保留它。一旦你得到一些排序的基本概念Coldfusion是一种可爱的语言编码英寸

+1

这是所有好的信息,但问题包括“在同一页面上”。 –

+0

所有好点的约翰,以及在变量集合左边#上的注释不是必需的。请使用 Benster

+0

@DanBracuk哈哈哈...非常好... –

相关问题