Output of CFC Query to Form

I’m a beginner with web apps. My application consists of 2 pages. On PAGE1, the user selects a customer name from a table and that name is sent to PAGE2 which calls CFC GET_CUST (via cfinvoke) with the customer name as parameter and GET_CUST returns a query (custDtls). PAGE2 has a 6 element form and the idea is to allow the user to edit the customer information which can then be updated to the database via another CFC. My question is how to load the output from GET_CUST into the form.

After much searching and experimentation, I can load the fields in the form using the code below and the output it to the screen to prove it:

<cfloop query="#custDtls#">
<cfset form.custname = #CustomerName#>

But, this is on the server, now how can I get the updated form to the client?


<cfloop query="#custDtls#">
<cfset theCustname = #CustomerName#>

<script language="javascript">
var #toScript(theCustname, "var1")#;
document.forms[0].custname.value = var1;
1 Like

Since you’re new to web apps, might I suggest taking some time and going through http://www.learncfinaweek.com/ to help familiarize yourself with CFML a bit better?

That said, you don’t have to loop through a query unless it has more than one record you want to display. If your query has a single record (which I assume it does in your example) then you can simply put the value directly into the form element, ala:

<input type="text" name="custName" value="#custDtls.CustomerName#">

No need to invoke JavaScript to give the field value in this case (though there are use cases for that, this doesn’t appear to be one of them).

For more examples of how to populate forms, might I suggest checking out some code on GitHub to help learn best practice ways of building forms (e.g. https://gist.github.com/bennadel/9753150). I would also suggest you check out either ColdBox (https://coldbox.ortusbooks.com/) or fw/1 (http://framework-one.github.io/documentation/4.2/) MVC frameworks and familiarize yourself with one (or both) of those if you’re serious about writing web apps.

And, finally, I would be remiss if I did not suggest you learn cfscript vs. tags (https://cfdocs.org/script, addl resources at the bottom of the page).

And, if you ever have questions about how to use a particular tag or function, https://cfdocs.org/ is a great place to start.

Need to test some code? Then give https://trycf.com/ a visit.


– Denny

Denny, thank you for the response. Yes, I have that book as well as a number of others and they are beginning to have their effect on my code as it has changed considerably as I learn and experiment. As for the particular piece of code you show, I do have a number of records to display and I’m curious about your suggestion. It makes no mention of a form and used as it is, it just creates a box on the screen with the correct information inside. Clearly I’ve misunderstood your intent so if you could clarify, I’d be most appreciative. Alan.

OK, I get it now. The line of code is within a CFOUTPUT which is within the FORM and that works a treat. Another learning…