Link to home
Start Free TrialLog in
Avatar of prairieits
prairieits

asked on

Process form action on browser close????

I have several pages, each has a form on it.  The problem I am having is that people will fill out the form, and rather than hit submit to then write the data to the db, they will just close the window, thinking that their changes are saved.  Is there some combination of body onunload and document.form1.submit(); that will force my page to process the form action page prior to closing the browser window?
 
Here's what I need:

1.  User fills out entire form
2.  User closes Internet Explorer (alt-F4, click on the x, whatever)
3.  document.formname.submit fires
4.  Since formname's action is itself, the form action happens, the SQL update statement inside the form action page happens, the rest of the form is displayed with the new information (as it would on a normal submit) for just a second and then the browser window closes

Steps 3 and 4 would happen in the timespan of a second or 2.  

Any ideas?  I need to be able to capture this data.

Thanks in advance!

Jerod
Avatar of lil_puffball
lil_puffball
Flag of Canada image

You can try onbeforeunload:

<body onbeforeunload="submitFormFunction();">
Never mind, you can't submit the form, but you can alert the user that their changes won't be saved:

<body onbeforeunload="return 'Your changes have not been saved! Please save your changes before closing.';">
Avatar of prairieits
prairieits

ASKER

The only problem with that is that if they actually do click the Submit button, they get the warning too, which would be good for them not to get if they have clicked the button that does save the data.  Do you have any idea on how to not show that warning if the submit button is clicked?

Thanks!

Jerod
ASKER CERTIFIED SOLUTION
Avatar of lil_puffball
lil_puffball
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or:

<body onbeforeunload="return 'Your changes have not been saved! Please save your changes before closing.';">

<input type=submit onclick="window.onbeforeunload=null;">
Thanks Lil pufball!

This will be very handy for me.

Take care.
Glad to help. :) Thanks for the points and the A!
Sorry to be a bother, but I can't get the warning to not come up when I actually click the submit button using either method.  Is there anything I would need to know to make either of those lines that create the submit button work?

Thanks in advance,
Jerod
Hmmm...seems to work for me:

<script>var showWarning=true;</script>

<body onbeforeunload="if(showWarning){return 'Your changes have not been saved! Please save your changes before closing.';}">

<form>
<input type=submit onclick="showWarning=false;">
</form>
Sorry.  I am stupid.  

And there you have it. :)

-Jerod