Simulate IE BACK Button with Coldfusion

How do I code a template so a person can return to the previous page (a form) and retain all of the variables already entered on the previous page ( a form).  Do I need a "submit" button on the sending page?  

I want to mimic the action of pressing the IE BACK button with a Button embedded on a cold fusion page.
Who is Participating?
gdemariaConnect With a Mentor Commented:

 Use javascript on the button..

<input type="button" onclick="javascript:history.back()">


 Here is a better way, this code framework shows you how to submit a form page to itself, process the action and then go to the next URL.  If there are any errors on the page, the processing "drops through" without being redirected and shows the values on the form just as the user entered them, without having been saved..

<!--- this is the primary key for the data on this page, expect it to be passed,
      if it is empty, the page will create a new record ----->
<cfparam name="form.contact_id" default="">  
<cfset variables.error = "">  <!---- this variable will hold an error message, if a problem occurs ---->

<cfif IsDefined('form.submit')>  <!-- the page has been submitted, process action --->
  <cftransaction action="BEGIN"> <!--- a transaction ensure everything on page is saved together or rolledback on error ---->
  <cftry>  <!---- catch any errors in the cfcatch below ----->
   <cfloop index="ii" list="lastname,firstname,email">  <!--- validate required fields are not empty --->
     <cfif len(form[ii]) eq 0>
          <cfthrow message="Please supply all required fields">
   <cfinclude template="owner_upd.cfm"> <!--- this file inserts or updates the data --->
   <cftransaction action="COMMIT">
  <cfcatch type="Any"> <!---- catch all errors, whether you cfthrow them or if happens in your sql statements ---->
     <cftransaction action="ROLLBACK">
     <cfset variables.error = cfcatch.message> <!---- set your error flag message variable ---->
  <cfif len(variables.error) eq 0> <!--- if no error, go to your next page ---->
     <cflocation url="/nextpage.cfm?ID=AddVariablesHere" addtoken="no">

<cfif len(variables.error) eq 0> <!--- this happens on first opening the file, get data ---->

   <cfquery name="getContact" datasource="#request.datasource#">
     select * from contacts
       where contact_id = #val(form.contact_id)#
   <!---- the next line will copy every column from the query into a FORM
          variable.  This will allow you to use the form scope below instead
              of the query name.  In case of an error, this allows your page
              to reload the entered values without saving them ---->
   <cfloop index="ii" list="#getContact.columnList#">
      <cfset form[ii] = getContact[ii][getcontacts.currentRow]>

<cfif len(variables.error) gt 0> <!--- if an error, show it ---->
  <cfoutput><span style="color:red">#variables.error#</span></cfoutput>
<!--- here is your form to get the data ---->

<form method="post">  <!---- No Action in form, page will post to itself ---->

 First Name: <input type="Text" name="firstName" value="#form.firstName#"><br><br>
 Last Name: <input type="Text" name="LastName" value="#form.lastName#"><br><br>

 Email: <input type="Text" name="email" value=""><br><br>

 <input type="Submit" name="submit" value="Save">

HarleyK1ddAuthor Commented:
gdemaria ... Not sure if my comment has already been added ,,, so I will add it again

Added a few points because you super-fast response really helped.  Also you get extra crecit for the "extra" suggestions.  Thank You.

 super, thanks much
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.