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#>
<cfoutput>#form.custname#</cfoutput>
</cfloop>

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

SOLVED!

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

<script language="javascript">
<cfoutput> 
var #toScript(theCustname, "var1")#;
</cfoutput>
document.forms[0].custname.value = var1;
</script>
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:

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

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. Multi-Step Form Demo In ColdFusion · GitHub). I would also suggest you check out either ColdBox (https://coldbox.ortusbooks.com/) or fw/1 (Getting Started with FW/1 | FW/1 - The Invisible Framework) 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 (CFScript Syntax Guide CFML Documentation, 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.

HTH

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

2 Likes