Solved

Coldfusion session or client variables won't save or pass between pages and reset to undefined

Posted on 2006-07-08
5
347 Views
Last Modified: 2013-12-24
I have spent hours searching the PAQs and haven't found a solution.  This is driving me crazy!  I'm extremely pressed for time and a solution, and need help right away!

I have a CFM form page, let's call it form.cfm, which submits to another CFM "processing" page, and let's call this page "post_signup.cfm".

The situation I'm attempting to get around is is that on the post_signup.cfm page I have a <cfmail> statement which sends the form data off via e-mail (as well as the data being posted to a SQL database), but if the user hits the REFRESH button while on the post_signup.CFM page, the data is resubmitted and re-emailed.  I've also tried "expire" the page with the no-cache meta commands in the hopes that the form.submit (submit button) status would revert to undefined.

I tried setting up the following on the post_signup.cfm page:

<cfif  NOT IsDefined('form.submit')><cflocation url="form.cfm"></cfif>  <!--- if the submit button from the form.cfm wasn't the way the came in, redirect them over to the form.cfm page --->

<cfif NOT isDefined('session.dataposted')>  <!--- Check to see if session variable is defined in the first place before attempting to assign values --->
<cfset session.dataposted=0>  <!--- Set the dataposted session variable to 0 for the first time the data is displayed --->
</cfif>

<cfif (session.dataposted EQ 1)> <!--- Check to see if we've been here before --->
<cfset session.dataposted=0> <!--- Reset the session variable back to 0 since it's SUPPOSED to  be a persistent var in case the user posts more data later --->
<cflocation url="index.cfm">   <!--- Go back to the home page --->
<cfelse>
<cfset session.dataposted=1>  <!--- Set the dataposted session variable so when/if the page is refreshed, the cfif above will redirect --->
</cfif>

The problem is that the session.dataposted variable is "undefined" each time the page is refreshed, so it's always set to 0.  (I check it with a CFOUTPUT of it's value further down the page).

I need to be able to show the data that was submitted while users view the post_signup.cfm page, so I can't have the page posting/emailing the data and redirecting to another page that doesn't show the FORM data.

If I try to predefine the session variable on the form.cfm page or even in the Application.cfm, then try to change it's value on the post_signup.cfm, the value is not stored, and when the page is refreshed the session.dataposted variable always has whatever it's initial value was.

My Application.cfm has session variables set to expire within 20 minutes and both sessionmanagement and  clientmanagement = Yes. (Not using client-side cookies in case user has them disabled in their browser).

I have even gone so far as testing a couple of pages with a Client.testvar variable instead of using session variables (but I'd really like to have both work):
page1.cfm:
<body>
<cfif not isDefined('Client.testvar')>
<cfset #Client.testvar# = "TESTING">
</cfif>

This is a test <CFOUTPUT>#Client.testvar#</CFOUTPUT>
<a href="test1a.cfm" target="_self">test 1a</a>
/body>

page1a.cfm:
<body>
<cfif not isDefined('Client.testvar')>
<cfset Client.testvar = "TESTING2">
</cfif>

This is a test <CFOUTPUT>#Client.testvar#</CFOUTPUT>
<a href="test1.cfm" target="_self">test 1a</a>
</body>

Because the Client.testvar is defined on page1.cfm, should I see it still set to "TESTING" on page1a.cfm?  page1a.cfm thinks that it's not defined, and defines it to be "TESTING2" and when clicking to go back to page1.cfm, it is reset again.

PLEASE HELP QUICKLY!  THANK YOU!
0
Comment
Question by:vitoasaro
  • 3
  • 2
5 Comments
 
LVL 36

Expert Comment

by:SidFishes
Comment Utility
"I need to be able to show the data that was submitted while users view the post_signup.cfm page, so I can't have the page posting/emailing the data and redirecting to another page that doesn't show the FORM data."

why not just pass the form data to the redirect page


submit.cfm
===============
<cfquery ....insert
<cfmail...

<cflock scope="Session" type="exclusive" timeout="20">
<cfset session.formfield1 = Form.somefield>
<cfset session.formfield2 = Form.someotherfield>
</cflock>

<cflocation url="post_signup.cfm">
==============

post_Signup.cfm
==============
<cflock scope="Session" type="readonly" timeout="20">
<cfset varformfield1 = session.somefield>
<cfset varformfield2 = session.someotherfield>
</cflock>

Thanks for signing up.
You submitted #varformfield1# and #varformfield2#



0
 
LVL 36

Expert Comment

by:SidFishes
Comment Utility
of course you should also just do a check prior to the insert

submit.cfm
===============

<cfquery name="eCheck" ...>

select emailaddy from mytable where emailaddy = '#form.emailaddy#'
</cfquery>

<cfif eCheck.recordcount Neq 0> <!--- then we've got a match --->

Oops...already registered
Click to go somewhere else

<cfelse>

<cfquery ....insert
<cfmail...

<cflock scope="Session" type="exclusive" timeout="20">
<cfset session.formfield1 = Form.somefield>
<cfset session.formfield2 = Form.someotherfield>
</cflock>

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


==============
0
 

Author Comment

by:vitoasaro
Comment Utility
Thanks for the quick reply, SidFishes.

The problem is that the session variables don't seem to be maintained across pages, so your example:

<cflock scope="Session" type="exclusive" timeout="20">
<cfset session.formfield1 = Form.somefield>
<cfset session.formfield2 = Form.someotherfield>
</cflock>

would appear to set those session variables on that page, but then when I jump to another page and attempt to output those variables in a <cfoutput></cfoutput> block, I'll get an error stating that those variables are undefined in the session.  Same happens if I use client variables.  So, for example, setting the variables as you show above, then linking to another page where I have:

<cflock scope="Session" type="exclusive" timeout="20">
<cfoutput>
Values of variables that should be set: #session.formfield1# #session.formfield2#
</cfoutput>
</cflock>

would prodcuce the error "formfield1 is undefined in SESSION"
0
 
LVL 36

Accepted Solution

by:
SidFishes earned 500 total points
Comment Utility

Do you have session mgmt turned on in application.cfm?

<CFAPPLICATION NAME="YOurApp" sessionmanagement="YES" clientmanagement="yes" setclientcookies="YES">
0
 

Author Comment

by:vitoasaro
Comment Utility
SidFishes:

Yes, I do have session mgmt turned on.

Another friend helped steer me in the right direction as well as your last question...  The URLToken wasn't consistently being passed along, so when I finally paused to look at the URL I noticed that part of it was missing.

Making sure that the addtoken="yes" was in all of my <cflocation> tags, and that all of my <FORM> and <CFFORM> tags were set up such as:
<CFFORM action="action.cfm?#Client.URLToken#" method="post"> were most important because the session state wasn't being maintained in the URL.

In some cases I had <CFFORM action="action.cfm?#Client.CFID#" method="post"> or didn't have it all, so without a reference to these session state variables, the server had no way of knowing that I was the same client (browser) trying to access my client/session variables.

Thanks again for the assist!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now