Hi all, CF noob again. I’m building a coldFusion driven web form and I’m able to validate the variables (aka form fields) just fine, but when an error is thrown (i.e., when the user doesn’t fill in a required field), it displays the error message just fine, but all of the fields that were previously filled in, go blank.
When there is an error, how do I make it so the fields that already had data in them, stay populated. Below is my current code:
<cfif IsDefined("FORM.submit")>
<cfif IsDefined("FORM.fName") AND FORM.fName eq "">
<cfset errMsg = "Please enter your first name">
</cfif>
<cfif IsDefined("FORM.lName") AND FORM.lName eq "">
<cfset errMsg = "Please enter your last name">
</cfif>
<cfif IsDefined("FORM.company") AND FORM.company eq "">
<cfset errMsg = "Please enter your company name">
</cfif>
<cfif IsDefined("FORM.email") AND FORM.email eq "">
<cfset errMsg = "Please enter a valid email address">
</cfif>
<cfif IsDefined("FORM.phone") AND FORM.phone eq "">
<cfset errMsg = "Please enter a phone number">
</cfif>
<cfif IsDefined("FORM.zip") AND FORM.zip eq "">
<cfset errMsg = "Please enter a zip code">
</cfif>
<cfif IsDefined("FORM.country") AND FORM.country eq "">
<cfset errMsg = "Please enter a country">
</cfif>
<cfif IsDefined("FORM.state") AND FORM.state eq "">
<cfset errMsg = "Please enter a state or province">
</cfif>
<cfif IsDefined("FORM.employees") AND FORM.employees eq "">
<cfset errMsg = "Please enter the number of employees your company has">
</cfif>
<cfset sSuccess = "Thank you" & #fName# & #lName# & "for requesting more information. One of our representatives will be in touch with you shortly" >
<cfoutput>
<h2>Get More Info</h2>
<p>*Required</p><br />
<cfif isDefined("errMsg")>
<span class="errormessage"><cfoutput>#errMsg#<br /><br /></cfoutput></span>
<cfelse>
<cfoutput>
#sSuccess#
</cfoutput>
</cfif>
<form action="#CGI.SCRIPT_NAME#" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><label for="fName">*First Name:</label></td>
<td><input type="text" name="fName" /></td>
</tr>
<tr>
<td><label for="lName">*Last Name:</label></td>
<td><input type="text" name="lName" /></td>
</tr>
<tr>
<td><label for="company">*Company:</label></td>
<td><input type="text" name="company" /></td>
</tr>
<tr>
<td><label for="email">*Email:</label></td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td><label for="phone">*Phone:</label></td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td><label for="zip">*Postal Code:</label></td>
<td><input type="text" name="zip" /></td>
</tr>
<tr>
<td><label for="country">*Country:</label></td>
<td><input type="text" name="country" /></td>
</tr>
<tr>
<td><label for="state">*US State/Canadian Province:</label></td>
<td><input type="text" name="state" /></td>
</tr>
<tr>
<td><label for="employees">*Number of employees:</label></td>
<td><input type="text" name="employees" /></td>
</tr>
<tr>
<td colspan="2" align="right" valign="top"><input type="submit" value="SEND" name="submit" /></td>
</tr>
</table>
</form>
</cfoutput>
<cfelse>
<cfoutput>
<h2>Get More Info</h2>
<p>*Required</p><br />
<form action="#CGI.SCRIPT_NAME#" method="post">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><label for="fName">*First Name:</label></td>
<td><input type="text" name="fName" /></td>
</tr>
<tr>
<td><label for="lName">*Last Name:</label></td>
<td><input type="text" name="lName" /></td>
</tr>
<tr>
<td><label for="company">*Company:</label></td>
<td><input type="text" name="company" /></td>
</tr>
<tr>
<td><label for="email">*Email:</label></td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td><label for="phone">*Phone:</label></td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td><label for="zip">*Postal Code:</label></td>
<td><input type="text" name="zip" /></td>
</tr>
<tr>
<td><label for="country">*Country:</label></td>
<td><input type="text" name="country" /></td>
</tr>
<tr>
<td><label for="state">*US State/Canadian Province:</label></td>
<td><input type="text" name="state" /></td>
</tr>
<tr>
<td><label for="employees">*Number of employees:</label></td>
<td><input type="text" name="employees" /></td>
</tr>
<tr>
<td colspan="2" align="right" valign="top"><input type="submit" value="SEND" name="submit" /></td>
</tr>
</table>
</form>
</cfoutput>
</cfif>