• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1782
  • Last Modified:

Cancel navigation, close etc...

I have a form that is rather long to fill in and I need a way to warn the user that the data has not been submitted before he/she leaves the page.

The main key for deciding if the form has been submitted is that a checkbox has been checked.

So far, I have a script that starts like this:

<script>
   function checkleave() {
    if (document.myForm.myCheck.checked) {
     alert ("Your data was not submitted, please click on the submit button before leaving.");
     // Help needed here...
     }
  }
</script>

And in my body:

<body onunload='checkleave()'>
<form name='myForm' ...

So what I would like to do is to return the user to my page after the alert...

Now it would be nice if the form was just submitted automatically and passthrough to where-ever the user wants to go without having to return to my page after the alert but that sounds hard and my brain is very tiny and kinda full right now so I'm not sure if I can handle that.
0
bganoush
Asked:
bganoush
  • 3
  • 3
2 Solutions
 
ZvonkoSystems architectCommented:
Did you try:

<script>
   function checkleave() {
    if (document.myForm.myCheck.checked) {
       //alert ("Your data was not submitted, please click on the submit button before leaving.");
       document.myForm.submit();
     }
  }
</script>

0
 
devicCommented:
you need here onbeforeunload event:
=============
<body  onbeforeunload="if(document.myForm.myCheck.checked) return 'You didn\'t saved your document' ;else return">
<form name='myForm'>
<input type=checkbox  name=myCheck>
<form>
0
 
bganoushAuthor Commented:
Zvonko,

Do you know if this works in all exit cases? The only ways out of a page seem to be clicking on a link, closing the window, a script redirection and typing a URL.... are there more ways?

-- Bubba
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
bganoushAuthor Commented:

Actually, I tried the submit but the problem is that it will perform the action from the form but will not redirect to the user's original URL... For instance, if the user click a link that goes to MSN.COM but the FORM has and action that goes to YAHOO.COM, then the orignal link is ignored...

Ex: (Try this, it's pretty funny.)

<HTML>
<SCRIPT LANGUAGE='Javascript'>
function checkleave() {
  if (document.myForm.myCheck.checked)
    document.myForm.submit();
}
</SCRIPT>
<BODY onunload='checkleave();'>
<FORM name='myForm' action='http://www.yahoo.com'>
<INPUT type='checkbox' name='myCheck'> I prefer Yahoo
</FORM>
<A HREF='http://www.msn.com'> Go to MSN </A>
</BODY>
</HTML>

Note that when you click on "Go to MSN" with the checkbox turned off, then it goes to MSN, no accounting for taste... But if you click on the checkbox "I prefer Yahoo" then clicking on the "Go to MSN" link will go to Yahoo but will not continue on to MSN.

In my application, my form submits via a C++ CGI and I just want the form to be submitted to the CGI and then I want to redirect to the user's original click or URL whatever was requested.

PS: devic,
ONBEFOREUNLOAD????      Never heard of it... can I get one in paisley or to match my mom's Royal Daulton?

-- Bubba
0
 
devicCommented:
hehe yes,  onbeforeunload, try example above:
0
 
bganoushAuthor Commented:
Alright,

That does work... just one more thing...  Can I assume that onbeforeunload works on older versions of IE and Netscape?  My HTML manual doesn't mention it and it's only about 2 years old... (No it's not HTML for Dummies!)

-- Bubba
0
 
devicCommented:
this is IE feature IE 4.0 +
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now