Page 1 of 2

onUnload to Save Data to Database

Posted: Sun May 30, 2004 7:39 pm
by Pyrite
Hi,

I have an onUnload Javascript Event on a page with a textarea, and I want to call a function or way to save the contents of a textarea to a database. Now I have the php code to do that, but am wondering a good way to do this. Currently, the onUnload event is in the body tag, and onUnload calls submit() for the form for the textarea. Then my script catches the POST var for the textarea and does the database call etc. But, the page that I click on to load doens't go through. Like it will save, and then stay on that same page. Just looking for a good way to handle this kind of thing.

Posted: Sun May 30, 2004 7:47 pm
by feyd
I'm not totally sure, but you may need to return true or false for the unload to continue..

Posted: Sun May 30, 2004 9:07 pm
by Pyrite
For php or javascript?

Posted: Sun May 30, 2004 11:13 pm
by feyd
the javascript.

Posted: Mon May 31, 2004 8:44 am
by Pyrite
Hmm, well I must not understand what to do, cause either one doesn't work. Here is what's up.

Code: Select all

<?php
/* Top of my php file */

if ($_POST['lnsave']) {
    $sql = "UPDATE tbl_lnbooks SET book_document='$lifenotebook' WHERE book_id='$bookid'";
    $db->Execute($sql) or die("Error: Query Failed to Save Your Life Notebook");
}
?>
<script>
function mySubmit() {
	document.fnotebook.onsubmit(); // workaround browser bugs.
	document.fnotebook.submit();
};
</script>

<body onunload="javascript:mySubmit();">
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" id="edit" name="fnotebook">
<p>
<textarea id="ta" rows="25" cols="10" name="lifenotebook" style="width:100%; height:100%">
<?=$book;?>
</textarea>
</p>
<p>
<input type="submit" name="btnsubmit" value="  Save  ">
<input type="hidden" name="bookid" value="<?=$bookid;?>">
<input type="hidden" name="lnsave" value="lnsave">
</p>
</form>
Now, I want this script to save the contents of the textarea when they hit the save button, and that works. And if they onUnload this page, to also save the contents of the textarea and continue on to the page that should take its place. In the mySubmit() javascript function I've tried return true and return false. Anyone have any better ways to do what I'm doing here.

Posted: Mon May 31, 2004 1:23 pm
by feyd
this works for me in IE6 and Mozilla...

Code: Select all

<?php ?><html>
	<script>
		function unloader()
		{
			document.forms[0].submit();
			return false;
		}
	</script>
	<body onunload="unloader()">
		<form action="http://www.google.com/search" method="get" name=f>
		<input type=hidden name=hl value=en><input type=hidden name=ie value="UTF-8">
		<input type=text name=q value="onunload test">&nbsp;<input type=submit value="Search">

		<a href="http://slashdot.org">slashdot.org</a>
	</body>
</html>

Posted: Mon May 31, 2004 1:52 pm
by Pyrite
So does that mean I should put my php/sql code in another file instead of the same one? I don't see how your example relates to my situation (sorry).

Posted: Mon May 31, 2004 2:16 pm
by feyd
doing a bit of testing: document.forms[0].onsubmit() for whatever reason, goes to the link, and not submitting (that I can tell)

Posted: Mon May 31, 2004 2:27 pm
by Pyrite
Is there any way to capture what link they want to go to, then after I do my database calls I can forward them onto that link?

Posted: Mon May 31, 2004 2:37 pm
by feyd
sure, uh, you could write a javascript to (onload) run through all links on the page adding an additional onclick event for them... and call your submitter function with the link's href as an argument. (that's probably the easier to set up, but more complicated (sorta) method. Otherwise, you'll need to add your submitter manually.. which isn't fun, or easily extensible.. (sorta)

Posted: Mon May 31, 2004 2:39 pm
by Pyrite
See the links are in another frame though, this whole thing takes place in 3 frames.

Posted: Mon May 31, 2004 2:42 pm
by feyd
that shouldn't be a problem..

Posted: Mon May 31, 2004 3:47 pm
by scorphus
Hey Pyrite,

While testing to see what can be done with the top object (JavaScript) I developed some more on that old example used to test the onUnload event.Take a look: http://scorphus.no-ip.com/lab/frames/

I enjoy playing with JavaScript and also had a particular interest on this matter since it can fit some needs of me me too.

I'll look for some other ways to do this, sometimes the JavaScript console returns me some uncaught exceptions. A great resource I use: http://devedge.netscape.com/central/javascript/

Um grande abraço,
Scorphus.

Posted: Mon May 31, 2004 3:58 pm
by Pyrite
scorphus, I think you may be done it, I'll test it and let you know!

Posted: Tue Jun 01, 2004 1:02 pm
by Pyrite
scorphus, if I understand your example corrently, I have to use the onClick=set Url2Goto thing on every link that links to the frame, if so, then that isn't really an option for me.