Solved

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

Posted on 2004-04-22
7
32,231 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Server with multiple sites and SSL 14 62
New OSQA server has a ton of fake users 4 55
Nameserver and MX Record 2 63
Problem to go to Web page 2 97
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
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 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…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. 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 : Go t…

910 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

22 Experts available now in Live!

Get 1:1 Help Now