Solved

Lost History On Window.History.Back() -- Javascript and ColdFusion

Posted on 2004-04-22
7
32,238 Views
Last Modified: 2013-12-20
Hello All,

I have a basic ColdFusion form with input boxes and combo boxes. The combo boxes are populated by CF queries. After submittal of the form, I do some server side error checking on the action page. If an error occurs, I display the error to the user and provide a link (simply window.history.back()) for him or her to return to the form to correct data. Sounds simple right? Well, here is where the complication comes in...at least for me anyhow. One of the combo boxes has an "OnChange" event that causes other fields to be populated. If I don't select anything from that combo box and force the error, the history is retained, but if something is selected from that combo box and an error occurs, the history disappears. Not only the combo box info is gone, but all the other information that I filled in as well...just sends me back to a blank form. Any help would be appreciated!

The code is pretty simple on the action page...just the CF validation and the window.history.back(); link. Below is the combo box.

<cfquery name="QRY_JOB_TYPES" atasource="#LAP_DATA_SOURCE#">
SELECT     JOB_TYPE_DESC, DEFAULT_DAYS
FROM     tab_JOB_TYPES
ORDER BY JOB_TYPE_DESC
</cfquery>

<select name="cmb_Type" size="1" onchange="Javascript:PopDates()">
<option value="" selected>
<cfloop query="QRY_JOB_TYPES">
<option value="#QRY_JOB_TYPES.JOB_TYPE_DESC#"><font class="ArialBlk8">#QRY_JOB_TYPES.JOB_TYPE_DESC# (average completion in #QRY_JOB_TYPES.DEFAULT_DAYS# days)</font>
</cfloop>
</select>

<script language="JavaScript">
function PopDates()
     <!--- Opens a popup that displays a please wait message and populates estimated start and end dates based on the job type selected --->
     {
     var typeindex = document.frm_JOB_ADD.cmb_Type.selectedIndex;
    var selectedtype = document.frm_JOB_ADD.cmb_Type.options[typeindex].value;
     var mypath = "JOB_POPULATE_DEFAULT_DATES_AND_USER.cfm?URL_TYPE=" + escape(selectedtype) + "&URL_BEG_DATE=" + escape(document.frm_JOB_ADD.txt_EST_START_DATE.value);
     window.open(mypath+"&random="+Math.random(),"mywin","scrollbars=1,left=190,top=160,width=160,height=80"); <!--- Opens Page And Refreshes Info From Server --->
     }
</script>


Thanks!
0
Comment
Question by:mts9
  • 3
  • 2
  • 2
7 Comments
 

Assisted Solution

by:riffraff5
riffraff5 earned 250 total points
ID: 10888049
when you submit your form, set each form field into a variable before processing any error checking...

<cfset firstname=form.firstname>
<cfset lastname=form.lastname>
etc.

On your form page, check for the existance of the variable to populate the field.

<cfinput type="text" name="firstname" <cfif isdefined("firstname")>value="#firstname#"</cfif> size="30" maxlength="30">

The more proper method is to use cfparam at top of  your form page and default each form field to "".

<cfparam name="firstname" default="">
<cfparam name="lastname" default="">

then set each form field as follows:

<cfinput type="text" name="firstname" value="#firstname#" size="30" maxlength="30">
<cfinput type="text" name="lastname" value="#lastname#" size="30" maxlength="30">

if the user submits the form and the variables are set on your action page as described above, the variables will remain constant regardless of the problems they may encounter.

hope this helps

0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 10888131
>>I do some server side error checking on the action page. If an error occurs, I display the error to the user and provide a link (simply window.history.back()) for him or her to return to the form to correct data

try some client side validation too

<form action="' method="" onSubmit="return validateForm(this);">

<script>

function validateForm(objForm){
var strErr="";
var objFocus="";
if (!objForm.formElement.value){
strErr+="FormElement is blank!\nThis is a required field.")
if(!objFocus){
objFocus=objForm.formElement;
}
..do all validation

if(strErr){
alert(strErr);
objFocus.focus();
}
return !strErr;
}

}

}
0
 

Author Comment

by:mts9
ID: 10888799
All,

Yes, I agree both of these fixes, but I guess I was looking for why my problem is occurring.

Thanks
Mike
0
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 

Expert Comment

by:riffraff5
ID: 10888936
Because you are not providing any way to maintain persistance of the user's inputs.
0
 
LVL 25

Accepted Solution

by:
James Rodgers earned 250 total points
ID: 10888941
>>but I guess I was looking for why my problem is occurring

nature of the web, pages are static and are referenced based on the actual page not dynamic changes made to a page, sometimes you can hit the back button on a browser and the form information will be there but not always and it doesn't seem to be any particular thing that decides it,

what i usually do is submit form pages to themselves, action="" or if the page is called myForm.cfm the form action=myForm.cfm, and at the top of the page
<cfif isDefined("form.submit")> <!--- or some other form field --->
do form processing/validation here

use cflocation, meta tag or window.location to redirect after success
</cfif>
if errors redisplay form with appropriate error message(s) highlighting incorrect fields,

works better than submit to new page and the first time the page is loaded there is no submission so submit is not defined and form displays without error messages
 
0
 

Author Comment

by:mts9
ID: 10889276
Points for both...thanks
0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 10889303
glad i could help


thanks for the points!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

820 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