onUnload to Save Data to Database

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

onUnload to Save Data to Database

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not totally sure, but you may need to return true or false for the unload to continue..
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

For php or javascript?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the javascript.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

See the links are in another frame though, this whole thing takes place in 3 frames.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that shouldn't be a problem..
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

scorphus, I think you may be done it, I'll test it and let you know!
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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.
Post Reply