Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forcing users to save on exit

Status
Not open for further replies.

ClifCamp

Technical User
Jul 24, 2001
23
US
I have an application which requires users to submit data they fill in on a form. The form is linked to a SQL database. Some users think it is enough to fill out the form and exit the page/browser without clicking the Submit button. The result is that some user data gets lost because they never submitted. Is there some code for catching users who haven't submitted when they to leave the page or close the browser? Any help will be greatly appreciated.
 
Ho do user exit the form? By clicking on exit button?

If so, set a hidden field on the form, default empty. When the form is submited, set the the value of this field to something. When user exit, check the value of hidden field. If it's empty, popup an alert.
 
In addition to Kendel's response, the browser will still close and the data will be lost, so you may want to use the onunload event to save their data to a cookie before you display the alert. Then when they go back into the page, you can prepopulate the data from the cookie.

Another option is to use the onbeforeunload event to warning them before the browser closes. Unfortunately, this only works in IE and has a hard-coded message in it you can't change.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Code:
<html>
<head>
<script type="text/javascript">
<!--
var submitted = false;
function onExit()
{
  if (!submitted) 
    alert("Oops! Please submit the form or your data will be lost.");
  return submitted;
}
window.onclose = onExit;
// -->
</script>
</head>
<body>
<form name="f" onsubmit="submitted = true;" action="page.ext" method="post">
<input type="text" name="i" /><br />
<input type="text" name="i2" /><br />
<input type="submit" value="Submit!" /><br />
</form>
</body>
</html>


--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
adam0101,

actually, i'm sure you'll be pleased to find that firefox supports onbeforeunload.

for example:
Code:
window.onbeforeunload = function() {
	return "really?";
}

however, i've found that when closing the window, you get the prompt twice. could be a bug in FF 1.0

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
>>Ho do user exit the form? By clicking on exit button?

>>If so, set a hidden field on the form, default empty. When >>the form is submited, set the the value of this field to >>something. When user exit, check the value of hidden field. >>If it's empty, popup an alert.

Adam, What I meant was to popup a confirmation let user know that data is not saved yet. Then user will have option to cancel the exit command and go back to the page to save data.
 
Kendel,
I understand what you were saying, but you can't cancel an exit command since the onunload event doesn't cancel. The browser will close no matter what. I was saying that in addition to your solution to display the alert, that ClifCamp should use the onbeforeunload event (which does cancel) or save the values to a cookie.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top