Solved

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

Posted on 2004-04-22
7
32,229 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
Comment Utility
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
Comment Utility
>>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
Comment Utility
All,

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

Thanks
Mike
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Expert Comment

by:riffraff5
Comment Utility
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
Comment Utility
>>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
Comment Utility
Points for both...thanks
0
 
LVL 25

Expert Comment

by:James Rodgers
Comment Utility
glad i could help


thanks for the points!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

744 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

17 Experts available now in Live!

Get 1:1 Help Now