Coldfusion 8 Bind data for self populating Forms

Sometimes you need a form to self populate some of the values based on input of one field. For example you can save a user data entry of address information if an id number is provided in a change of address form. The data will still be able to be edited in the form should changes be needed. I got the idea from the CF8 application developer book v2 (ch 34) and from Ben Forta’s blog. I then enhanced what they provided to suit my needs. Below is a generic example of the code values needed with non related html and other stuff stripped out. This could be enhanced with CSS and proper form structure using labels, fieldsets, etc. It is provided as merely guidance.

<code>
EMP_COA.cfm (change of address form)
———————————————————-
<cfform name=”id_form” action=”EMP_COA_UPDATE.cfm” method=”post”>
emp_id <input type=”text” name=”emp_id” /><br />

<!— stuff that will auto populate based on emp_id —>
emp_name <input type=”text” name=”emp_name /><br />
emp_address <input type=”text” name=”emp_addr” /><br />
emp_city <input type=”text” name=”emp_city” /><br />
emp_state <input type=”text” name=”emp_state” /><br />
emp_zip <input type=”text” name=”emp_zip” /><br />
emp_phone <input type=”text” name=”emp_phone” />
<input type=”submit” value=”submit”/>
</cfform>

<cfdiv bind=”url:EMP_COA_RESULTS.cfm?emp_id={emp_id@blur}” />

EMP_COA_RESULTS.cfm - (loads address data to the COA form)
——————————–
<cfparam name=”emp_id” default=”">

<cfif #len(trim(url.emp_id))# EQ “9″>
<!— if the value passed has 9 characters use cfinvoke to call a CF component emp_coa.cfc —>

<cfinvoke component=”emp_coa” method=”searchEmpId”
emp_id=”#url.emp_id#” returnVariable=”results”>

<!— if the returned results are not equal to 1 row throw an error —>
<cfif #results.recordCount# NEQ 1>
<script type=”text/javascript” language=”javascript”>
alert(”Possible incorrect Employee Id, please check”);
return false;
</script>
</cfif>
<!— Return the results to the form using javascript element names and values from the results query.—>
<script type=”text/javascript” language=”javascript”>

<cfoutput query=”results”>
document.id_form.emp_name.value=”#ucase(emp_name)#”;
document.id_form.emp_addr.value=”#emp_addr#”;
document.id_form.emp_city.value=”#emp_city#”;
document.id_form.emp_state.value=”#emp_state#”;
document.id_form.emp_city.value=”#emp_zip#”;
document.id_form.emp_phone.value=”#emp_phone#”;
</cfoutput>

</script>
</cfinvoke></cfif>

EMP_COA.cfc (Does the query based on the COA form)
—————————-
<cfcomponent>

<cffunction name=”searchEmpId” output=”false” returnType=”query” access=”public”>
<cfargument name=”emp_id” type=”string” required=”true”>

<cfset var q = “”>

<cfquery name=”q” datasource=”datasourcename”>
select *
from EMPLOYEES
<cfif len(trim(arguments.emp_id))>
where upper(emp_id) =
<cfqueryparam cfsqltype=”cf_sql_varchar” value=”#ucase(arguments.emp_id)#”>
and rownum < = 1
</cfif>
order by emp_id
</cfqueryparam></cfif></cfquery>

<cfreturn q>
</cfreturn></cfset></cfargument></cffunction>
</cfcomponent>

EMP_COA_UPDATE.cfm (performs the actual update)
——————————-
<cfquery name=”upd_emp” datasource=”datasourcename”>
update EMPLOYEES
set emp_name = ‘#Form.emp_name#’,
emp_addr = ‘#Form.emp_addr#’,
emp_city = ‘#Form.emp_city#’,
emp_state = ‘#Form.emp_state#’,
emp_zip = ‘#Form.emp_zip#’,
emp_phone = ‘#Form.emp_phone#’
where upper(emp_id) = ‘#UCASE(Form.emp_id)#’
</cfquery>
…. do other stuff to confirm the changes to the user

</code>

Leave a Reply

You must be logged in to post a comment.