I have two forms. In form1 both name and address must be entered. If one of these entries are missing, an error message is displayed after doing server side validation. If no errors, the results entered in form1 should be displayed in form2. I executed CFLOCATION after successful validation, but the data entered in form1 are not passed to form2. I get the message txtName and txtAddress are undefined in form (2). How can I pass data from the first screen screen to another after server side validation was successful? Any suggestion is highly appreciated. Below please find my code
Form1
<cfif isDefined("form.btnSubmit")>
<cfif len(trim(#form.txtName#)) GT 0 and len(trim(#form.txtAddress#)) GT 0>>
<cflocation url="form2.cfm" addtoken="true">
<cfelse>
<H3>Name and address must be entered</H2>
</cfif>
</cfif>
<cfform action="form1.cfm" method="post">
User ID:<cfinput type="Text" name="txtName"><br>
Phone: <cfinput type="Text" name="txtAddress"><br>
<cfinput type="submit" name="btnSubmit" value="Validate"><br>
</cfform>
Form2
<H2>You made the following entries </H2>
<p> Name: <cfoutput>#form.txtName#</cfoutput></p>
<p> Address: <cfoutput>#form.txtAddress#</cfoutput></p>
This answer is in response to this comment, "Is the method described above , doing validation in form2, the best practice for doing server side validation?"
There are at least three methods for server side validation of form fields. In order of the number of pages required, we'll start with the 1 page method. All the code is on one page. It goes something like this:
For the 2 page method, PageWithForm.cfm submits to FormProcess.cfm. The code on FormProcess.cfm will be almost exactly as described above. The only difference is that
becomes
The 3 page method has PageWithForm.cfm, FormValidate.cfm, and FormProcess.cfm. This seems to be what you are attempting. The question is, how does FormValidate.cfm pass the values to FormProcess.cfm. There are at least 3 methods.
I like session variables the least because they can be changed unexpectedly. I prefer the new form to url variables, but that's just me.
All the methods I described work. Sometimes the best one depends on the situation at hand and sometimes it simply doesn't matter. I rarely use the one page method. I'll normally use the two page method. But that's just me.