Search code examples
coldfusioncoldfusion-9

How to set value from a query to an input textbox


When a user clicks edit of a specific row in a table, I want the input textboxes in the same page to be filled with that row's details. I tried this but it did not work.

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

</cfoutput>

Solution

  • Try this as an example to get you going....

    <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>
    

    I've put comments in the code around some optional improvements.

    Also note that...

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

    will be causing you problems.

    <cfset username = getDataForEdit.username>
    

    Will create/set a variable username equal to the value of the username column in your query. The # tags are not necessary in this context. # prints out the value of a variable, so you could have done this...

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

    Which would print the value of the username column into a string of text (the only thing in the string being the value), and the string of text would have been assigned to the variable username.

    form
    

    is a reserved word, as it is a structure (it's worth looking up structures) that contains all of the form variable data posted to your page. You can inspect form by using cfdump

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

    To print out one of the values inside form you can do

    #form.username#
    

    So (and very confusingly when you're a beginner)...

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

    Will create a variable with the name that corresponds to the value of the form field username posted to your page, and fill it with the username from your query. You really don't want that.

    Keep at it. Once you get a few basic concepts sorted Coldfusion is a lovely language to code in.