Coldfusion Server Side Form Validation – how to display data entered in one form into another form

1.4k Views Asked by At

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>
3

There are 3 best solutions below

1
On

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:

 if (a form was submitted)
 validation code goes here

 if (you had good data)
 code to process form fields goes here
 else
 code for problems with form fields goes here

 else // no form submitted
 code to produce form goes here.

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

 code to produce form goes here

becomes

 code for no form submitted goes here.

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.

  1. Make them session variables.
  2. Make them url variables and use cflocation
  3. Create another form in FormValidate.cfm, transfer the original values to hidden fields and submit it with javascript.

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.

2
On

cfloction does not submit a form, it just redirects the user to a new page. If you want the data that the first form submitted to display on the second form, then add your second form to the the page you currently have your cflocation on, and do the the verification there. If the required data is there, then populate the second form with the data. Otherwise you can send them back to the first form.

0
On

I think what you are trying to ask is closer to this:

form1.cfm

<cfparam name="url.message" default="">

<cfif url.message EQ 1>
    <H3>Name and address must be entered</H3>
</cfif>

<cfform action="form2.cfm" method="post">
   User ID:<cfinput type="Text" name="txtName"><br>
   Phone: <cfinput type="Text" name="txtAddress"><br>
   <cfinput type="submit" value="Validate"><br>
</cfform>

form2.cfm

<cfif len(trim(form.txtName)) EQ 0  and len(trim(form.txtAddress)) EQ 0>
     <cflocation url="form1.cfm?message=1" addtoken="false">
</cfif>


<H2>You made the following entries </H2>
<p> Name: <cfoutput>#form.txtName#</cfoutput></p>
<p> Address: <cfoutput>#form.txtAddress#</cfoutput></p>

On Checking for required fields

In this day an age, there (at least) 5 major approaches

  1. Use a jQuery library to enforce required fields
  2. Write custom Javascript to validate required fields
  3. Use HTML 5 attributes to enforce required fields
  4. Use <cfform> to enforce required fields
  5. Have the response page deal with the required fields

There are trade offs with each approach. Your question seems is approach 5, but it also includes some of the work for approach 4