Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Passing data between pages

New Here ,
May 09, 2011 May 09, 2011

Hello,

I have two pages, form.cfm, and validation.cfm. I wish to check in validation the values submitted by form. In case of error or wrong values, I'd redirect the user back to form, with wrong fields highlighted. Now, I don't know how to pass these values between the pages. If I put them into session, the user will keep them for a longer time than expected. I could destroy those values once values are validated as ok, but am unwilling to write/test/check too much. But since sessions rely on cookies, am I to write a page for cookies disabled users, and one for javascript disabled users?

I could put validation and form on the same page, but don't want either to make my page a mess.

Any suggestion, if any?

Thanks!

TOPICS
Getting started
6.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 09, 2011 May 09, 2011

It sounds to me like what you want to use is just a basic HTML form. I'd suggest reading up on those before you go delving into the CF side.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2011 May 09, 2011

Thanks Owain,

could you be more precise about why are you suggesting to study HTML forms?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 09, 2011 May 09, 2011

Because they're the absolute basis of sending data around the internet, and are the *only* method that a browser can use for sending data to your website without having to use Flash or Java.

There's no point delving into complicated ColdFusion code verifying data if you don't even have the data between one page and another yet. The W3 Schools tutorials are normally pretty good.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2011 May 09, 2011

I think I have a fair knowledge of input controls (not complete!), do you

mean that I could pass values between pages through the form scope / post

method?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 09, 2011 May 09, 2011

That is the standard way of moving user-submitted data between pages, yes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 09, 2011 May 09, 2011

While there are many ways to accomplish this I offer one of the most simple and should give you a place to start

Try something like this:

<!--- form.cfm --->
<form action="validate.cfm" method="post">
<input name="myTextBox" size="25" maxlength="25" type="Text" <cfif IsDefined("session.myTextBox")>value="#session.myTextBox#"</cfif>>
<input type="Submit">
</form>

<!--- validate.cfm --->
<cfset goodValueList='valueOne,valueTwo,valueThree'>
<cfif ListFind(goodValueList,form.myTextBox) gt 0>
<cfquery name="putData" datasource="#session.myDataSource#">
   Insert into myTable(myText)
   Values (<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="form.myTextBox">)
</cfquery>
<cfset session.myTextBox=''>
<cfelse>

<cfset session.myTextBox=form.myTextBox>

<cflocation url="form.cfm">
</cfif>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
May 09, 2011 May 09, 2011

I wrote a Form Validation and Flow -- One Technique tutorial several years back that I think will point you in the right direction. This tutorial focuses strictly on server-side validation which is a must whereas most of the responses here focus on client-side validation. You really need both -- one prevents hackers from posting cross-site scripting (XSS) and SQL injection code, the other provides much more user friendly form handling with instant feedback to the user.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 05, 2011 Jun 05, 2011
LATEST

Silvestro wrote:

Hello,

I have two pages, form.cfm, and validation.cfm. I wish to check in validation the values submitted by form. In case of error or wrong values, I'd redirect the user back to form, with wrong fields highlighted. Now, I don't know how to pass these values between the pages. If I put them into session, the user will keep them for a longer time than expected. I could destroy those values once values are validated as ok, but am unwilling to write/test/check too much. But since sessions rely on cookies, am I to write a page for cookies disabled users, and one for javascript disabled users?

I could put validation and form on the same page, but don't want either to make my page a mess.

Any suggestion, if any?

You could use session variables and, instead of bothering about deleting them, simply update them dynamically. The following pseudocode is more or less similar to Steve's approach.

form.cfm

========

<cfif isDefined("session.isFormSubmitted") and session.isFormSubmitted>

<div>
<!--- loop through the fields, and print a validation message where the field's validation status is 0--->
<cfloop collection="#session.status#" item="field">
<cfif session.status[field] is 0>
<cfoutput>
<p>#field#: #session.message[field]#</p>
</cfoutput>
</cfif>
</cfloop>
</div>

<!--- (re-)populate fields with the values stored in session scope --->
<cfform action="validation.cfm">
Field 1: <cfinput type="text" name="field_1" value="#session.value.field_1#"><br>
Field 2: <cfinput type="text" name="field_2" value="#session.value.field_2#"><br>
Field 3: <cfinput type="text" name="field_3" value="#session.value.field_3#"><br>
<cfinput type="submit" name="sbmt" value="Send">
</cfform>

<!--- reset the value --->
<cfset session.isFormSubmitted = False>

<cfelse>

<cfform action="validation.cfm">
Field 1: <cfinput type="text" name="field_1" required="true" message="You must enter a value for field 1."><br>
Field 2: <cfinput type="text" name="field_2" required="true" message="You must enter a value for field 2."><br>
Field 3: <cfinput type="text" name="field_3" required="true" message="You must enter a value for field 3."><br>
<cfinput type="submit" name="sbmt" value="Send">
</cfform>

</cfif>

validation.cfm

============

<cfset formValidationStatus = 1>
<cfset session.isFormSubmitted = True>

<cfset session.value.field_1="">
<cfset session.value.field_2="">
<cfset session.value.field_3="">
<cfset session.status.field_1=0>
<cfset session.status.field_2=0>
<cfset session.status.field_3=0>
<cfset session.message.field_1="">
<cfset session.message.field_2="">
<cfset session.message.field_3="">


<cfif isDefined("form.sbmt")><!--- then form submitted --->
    <!--- store the values in session scope --->
    <cfset session.value.field_1=form.field_1>
    <cfset session.value.field_2=form.field_2>
    <cfset session.value.field_3=form.field_3>
   
    <!--- validate for each and every field. Set the status to 1 if validation succeeds, to 0 if it fails --->
    <!--- I'll illustrate with just field_1. Suppose it represents a year between 1900 and 2020. --->
    <cfif (len(trim(form.field_1)) EQ 4) and (isNumeric(form.field_1)) and (form.field_1 GTE 1900 and form.field_1 LTE 2020)>
        <cfset session.status.field_1=1>
    <cfelse>
        <!--- If status of a field is 0, then include the validation message to the seesion struct --->
        <cfset session.message.field_1="Field 1 must be a year between 1900 and 2020.">
    </cfif>
   
    <!--- Loop through all the status values. If at least one of the fields fails to validate, then set formValidationStatus to 0, meaning a return to the form page --->
    <cfloop collection="#session.status#" item="field">
        <cfif session.status[field] EQ 0>
            <cfset formValidationStatus = 0>
            <cfbreak>
        </cfif>
    </cfloop>
</cfif>

<cfif formValidationStatus EQ 0>
    <cflocation url="form.cfm">
<cfelse><!--- all submitted fields valid --->
<!--- proceed with business logic --->
</cfif>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources