onUnload to Save Data to Database
Moderator: General Moderators
- 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
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.
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.
- Pyrite
- Forum Regular
- Posts: 769
- Joined: Tue Sep 23, 2003 11:07 pm
- Location: The Republic of Texas
- Contact:
Hmm, well I must not understand what to do, cause either one doesn't work. Here is what's up.
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.
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>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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"> <input type=submit value="Search">
<a href="http://slashdot.org">slashdot.org</a>
</body>
</html>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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)
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
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.
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.