Persist form values on reload w/ jQuery & ColdFusion

A designer friend presented me with a somewhatlong and complicated HTML form. It is a survey/contest entry for employees of a specific industry, with dozens of fields which include regular text inputs, text areas, and radio buttons.

My task is to create a routine to submit this form via e-mail and store the contents in the database. ColdFusion makes that easy with the built-in #form# scope. And for validation, I simply made his HTML < form > into a <cfform>, and all of the <input> and < textarea > elements into <cfinput> and <cftextarea>, adding 'required="true"' and a validation rule and/or message for each. Whipping up a quick server-side validation for the required fields was also a snap with CF.

Then it occurred to me - if there is in fact some error with the user submission, we will return the user to the form showing an error message at the top of the page.

However, unless we somehow persist the values that were entered, the form will be blank, and the user will no doubt be a little frustrated. This is a long form!

one method would be to create <cfparam> values for each element in the form, and then give each input the a value like value='#form.thisFieldName#" - that's common practice, and works great, but I don't want to do the tedious work. Also, I will be passing this form back to the designer, and don't want him to have to create the default value if he decides to change the names of any inputs, or at other input to the form.

So, I came up with this.

Assuming you already have jQuery in your page, this little block of code will loop through all of the posted values in the ColdFusion form scope, then, using jQuery, will assign the given value to any form element with a matching "name" attribute, allowing us to easily re-populate all the form fields in the page, in one fell swoop with javascript.

Since i only had input / textarea / select / radio button inputs, this just handles those types, but could easily be ammended to include checkboxes and more.

<!--- if form scope exists --->
<cfif isDefined('form.fieldnames')>
    <cfsavecontent variable="hcode">
    <script type="text/javascript">
    $(document).ready(function(){
    <cfloop list="#form.fieldNames#" index="ff">
    <cfset rawVal = form[ff]>
    <cfset ffVal = jsStringFormat(form[ff])>
    <cfoutput>
    $('input[type="text"][name="#lcase(ff)#"]').val('#ffval#');
    $('select[name="#lcase(ff)#"]').val('#ffval#');
    $('textarea[name="#lcase(ff)#"]').text('#ffval#');
    $('input[type="radio"][name="#lcase(ff)#"][value="#rawval#"]').attr('checked','checked');
    </cfoutput>
    </cfloop>
    //end jQuery
    });
    </script>
    </cfsavecontent>
<cfhtmlhead text="#hcode#">
</cfif>


Yes, this makes a big long looped jQuery script in the head of the user's page. But in this instance, it is working perfectly, with no noticeable slowing of page load, or other drawbacks that i can see.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
You'll want to make sure to use jsStringFormat() around your dynamic values so you don't run into problems with special characters.
# Author Tony Nelson | 12/8/10 3:31 PM
Thanks Tony, good point - I'll add that to the demo code.
# Author Michael Evangelista | 12/8/10 3:42 PM
@Michael -- are you posting the form back to the same page? If so you could try using the 'preserveData' attribute of cfform.

This is one of those instances where Model-Glue (and I'm sure other frameworks) shine since you can do things like:

value="#viewstate.getValue('firstName', query.firstName)#"

which would get the POSTed firstName (if it exists) or default to query.firstName (for an edit form that is pre-populated from a query).
# Author Todd Sharp | 12/8/10 7:12 PM
Oooh cool. I didn't know about "preserveData" (i hardly ever use CFFORM, much preferring jQuery validation). Thanks for the tip!
# Author Michael Evangelista | 12/8/10 7:26 PM
Ditto re: cfform. But somehow I remembered that attribute existing...
# Author Todd Sharp | 12/8/10 8:13 PM
No need for server side. Just jQuery alone. HTML 5 data storage works on IE 8+ and all major browsers. I made some code using this technique at http://www.jasonsebring.com/dumbFormState that is free to use.
# Author Jason Sebring | 4/17/11 9:13 PM
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista