文章作者 : linkfoxo [ webmaster@cfwindow.com ] Web URL : http://www.cfwindow.com
上载日期 : 2000-12-18
如何在用户刷新页面时候不丢失用户输入的资料呢?
The Situation: You have created a form that you want to let the user return to -- but with the information they entered still in the form fields. Right now, it comes back blank.
The Solution: You will need to use persistent variables. While normal variables go away when your browser changes pages (hence the problem!), persistent variables stick around and can be accessed. At present, ColdFusion 4.x supports cookies, client, server, session, and application variables.
Which you use depends on a number of things. Do you need to store this information for only a few hours? Does it need to be for longer? Once you've decided on the type of persistent variable you want, you'll need to rework your form page. Here I'm using session variables.
<cfparam name="session.firstName" default="">
<cfparam name="session.lastName" default="">
<cfparam name="session.city" default="">
<form action = "doMyForm.cfm" method="post">
First name: <input type ="text" name = "firstname" value="#session.firstname#">
<br>
Last name: <input type ="text" name = "firstname" value="#session.lastname#">
<br>
City: <input type ="text" name = "firstname" value="#session.city#">
<br>
</form>
Next, your form processing page needs to save all the form variables into the kind of persistent variables you've chosen. Something like...
<cfsetting enablecfoutputonly = "yes">
<cfif isdefined("form.fieldnames")>
<cfloop list="#form.fieldnames#" index="field">
<cfset setvariable("session.#field#","#evaluate("form."&"#field#")#")>
</cfloop>
</cfif>
<cfsetting enablecfoutputonly = "no">
Now, whenever your user goes back to the form page, the form will populate itself with the values contained in your persistent variables. The <cfparam> tags on the form page ensure that if this is the user's first time at the form, s/he will see a blank page ready for them to enter their info.
|