CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 BeforeUnload event to prevent navigation away from page on error

Print topic Send  topic

Author Message
andy


Posts: 183
Posted: 10/30/2007, 11:13 AM

Hi

I want to intercept the user with an alert box before they leave the page.

Based on ftubino->Brazil's suggestion at:
http://forums.yessoftware.com/posts.php?post_id=50559&s_keyword=alert

Insert a label in the form "myform" named "mylabel" rememberng to set the label type=HTML
Then in the form's BeforeExecuteUpdate event (or other appropriate event) add:

   
if (condition goes here) {  
$script="<script language=javascript>    
   alert ('This is the warning message to alert the user of the problem.' );    
  </script>";    
$myform->mylabel->setValue($script);  
}

This works fine.

HOWEVER, I want to test for conditions if the user leaves the page (e.g. presses form's Cancel button or clicks on a menu link provided elsewhere on the page.

If I apply the same code but this time with
* mylabel on the PAGE (not in a form)
* using the BeforeUnload event

Guess what... Yes you got it, it doesn't work! Yikes.

Anyone got any suggestions on how to intercept the user with an error message to prevent them from navigating away from a page if a condition is not met?
_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message
andy


Posts: 183
Posted: 11/07/2007, 2:08 AM

I have found a solution.

It is entirely javascript and does not use the CCS BeforeUnload event.
Instead it is based on the javascript onbeforeunload event.

Add the following code to the head section of your page:

  
<script language="JavaScript" type="text/javascript">  
  
var unsaved = false;  
var unsubmitted = true;  
window.onbeforeunload = function(){if((unsaved==true) && (unsubmitted==true)){return "You have unsaved data. Are you sure you wish to leave this page.";}};  
  
</script>  

For each form field add unsaved = true; against the onchange event on the Format tab of the field's Properties box.

Each form field will then have the following code:

  
<input onchange="unsaved = true;" value="{Memorandum}" name="{Memorandum_Name}">  

Then for each form button (Save, Cancel, Delete...) add unsubmitted=false; against the onclick event on the Format tab of the field's Properties box.

Each form button will then have the following code:
  
<!-- BEGIN Button Button_Insert --><input class="Button" onclick="unsubmitted=false;" type="submit" value="Add" name="{Button_Name}"><!-- END Button Button_Insert -->  

The javascript code displays a message box asking the user whether they wish to leave the page before saving only if
* a field value has been changed (the user has started to add to the form)
* the form isn't being submitted or cancelled by the user by clicking on the buttons

The only fly in the ointment, which I haven't started to think about yet, is if you set a field to display a default value. If the user lands on the page, makes no changes and attempts to navigate away, he will be prompted becuase javascript thinks the default value has changed from null to the default value. I'm sure there's an easy js solution...
_________________
Andy

RAD tools for rich UI controls:
http://www.koolphptools.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.