?
Solved

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

Posted on 2004-04-22
7
Medium Priority
?
32,266 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 750 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two 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 750 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The purpose of this video is to demonstrate how to connect a WordPress website to Google Analytics. This will be demonstrated using a Windows 8 PC Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php :…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 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