The cfform
preservedata
attribute tells ColdFusion to continue displaying the user data in a form after the user submits the form. Data is preserved in the cfinput
, cfslider
, cftextinput
, and cftree
controls and in cfselect
controls populated by queries. If you specify a default value for a control, and a user overrides that default in the form, the user input is preserved.
You can retain data on the form when the same page contains the form and the form's action code; that is, the form submits to itself. You can also retain the data if the action page has a copy of the form, and the control names are the same in the forms on both pages. (The action page form does not need to be identical to the initial form. It can have more or fewer elements than the initial page form; only the form elements with identical names on both pages keep their data.)
Note: The preservedata
setting on the action page controls the preservation of the data.
For example, if you save this form as preserve.cfm, it continues to display any text that you enter after you submit it, as follows:
<cfform action="preserve.cfm" preservedata="Yes"> <p>Please enter your name: <cfinput type="Text" name="UserName" required="Yes"><p> <input type="Submit" name=""> <input type="RESET"> </cfform>
When you use the preservedata
attribute, follow these guidelines:
cftree
tag, the preservedata
attribute causes the tree to expand to the previously selected element. For this to work correctly, you must also set the completePath
attribute to True.
preservedata
attribute has no effect on a cfgrid
tag. If you populate the control from a query, you must update the data source with the new data (typically by using a cfgridupdate
tag) before redisplaying the grid. The grid then displays the updated database information.