Solved

Process form action on browser close????

Posted on 2004-04-29
10
1,051 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:prairieits
  • 6
  • 4
10 Comments
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10954182
You can try onbeforeunload:

<body onbeforeunload="submitFormFunction();">
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10954218
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.';">
0
 
LVL 4

Author Comment

by:prairieits
ID: 10954305
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
0
 
LVL 12

Accepted Solution

by:
lil_puffball earned 500 total points
ID: 10954465
Sure:

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

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

<input type=submit onclick="showWarning=false;">
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10954467
Or:

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

<input type=submit onclick="window.onbeforeunload=null;">
0
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.

 
LVL 4

Author Comment

by:prairieits
ID: 10954597
Thanks Lil pufball!

This will be very handy for me.

Take care.
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10954606
Glad to help. :) Thanks for the points and the A!
0
 
LVL 4

Author Comment

by:prairieits
ID: 10955036
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
0
 
LVL 12

Expert Comment

by:lil_puffball
ID: 10955074
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>
0
 
LVL 4

Author Comment

by:prairieits
ID: 10955097
Sorry.  I am stupid.  

And there you have it. :)

-Jerod
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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

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

23 Experts available now in Live!

Get 1:1 Help Now