2017-09-13 308 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哈哈哈...非常好... –

相關問題