A Wild Way to Show Default Values in Your Form

Here’s another interesting technique. Let’s say I have a form.  I need to set some default values in the form, just in case someone submits it with missing info, and they’re redirected back to complete it. We need to save their info, right? I doubt they’d enjoy filling out the entire form again.

This is another situation where I use <cfparam>. You can simply <cfparam> some empty values on your form page. When the user first loads the form, they’re empty. If they come back due to an incomplete form submission, the values will be retained and displayed.

This time, I shall show an entire example.

The form:

<form name=”frmLogin” action=”index.cfm?action=tomlinson.setForm” method=”post”>

First Name: <input class=”req” type=”text” name=”usrID” size=”20″ value=”#variables.frmData.usrID#” />

Password: <input class=”req” type=”password” name=”usrPwd” size=”20″ value=”#variables.frmData.usrPassword#” />

Email: <input class=”req” type=”text” name=”usrEmail” size=”20″ value=”#variables.frmData.usrEmail#” />

<input class=”req” type=”checkbox” name=”agree” size=”20″ #variables.frmData.agreeToPlan#/> Agree to our plan?

<input class=”button” type=”submit” name=”submit” value=”submit”/>

</form>

What’s up wit dis? We’re just outputting some default values in our form. No more – no less.

Here’s the part I want you to check out. I have a method whose job is to setup some default data, then return it for display in our form:

<cffunction name=”getFrmData” access=”public” returntype=”struct” output=”false”>

<cfargument name=”req” type=”struct” required=”true”/>

<cfargument name=”frm” type=”struct” required=”true”/>

<cfargument name=”url” type=”struct” required=”true”/>

<cfscript>

var frmData = structNew();

frmData['usrID'] = “”;

frmData['usrPassword'] = “”;

frmData['usrEmail'] = “”;

frmData['checked']['yes'] = “checked=”"checked”"”;

frmData['checked']['no'] = “”;

frmData['agreeToPlan'] = frmData.checked[structKeyExists(arguments.frm, "agree")];

structAppend(frmData, arguments.frm);

</cfscript>

<cfreturn frmData/>

</cffunction>

This is all well and good, but you might be wondering what the hell with the structAppend().

structAppend(frmData, arguments.frm);

Here’s the deal. When the form first loads, it loads empty strings as default values. If the form was submitted, this method actually gets executed a second time around. And oh yes, there IS an existent form scope that second time around. And oh yes, the structAppend() overwrites those blank values.

So what are we left with after being redirected back to our form? Yep, the values that were submitted in the original form post. The structAppend overwrote our blank values in the frmData struct. And they will now display in for form so the user needs not enter them a second time.

This one is actually kinda sexy I think. If you have a cool way to handles these sorts of things, please comment. :)

Oh, and thanks to my mentor, Adam, for showing me this cool technique.

1 comment so far

  1. KC on

    I like it.


Leave a reply