By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

onunload and XMLHttpRequest - going nuts

P: n/a
I have been at this for hours now. Pulling my hair out.

I have a standard php page that has an onunload event on it. The
onunload event sends a xmlhttprequest that triggers a database update,
for simplicity lets say it updates a table:
UPDATE testtable SET testfield = '".$_GET['rand']."' where id = 1

This works just fine.

Then the next page you navigate to is supposed to call the testfield
from the database and it should display the newly updated value. It is
not. If you refresh the next page again you now get the correct newly
inserted value.

Does the onunload event prefetch the next page prior to executing the
onunload function???

Anyone have any ideas?? I cant find anything in the archives about
this.

Thanks.

Feb 1 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
tg****@gmail.com wrote:
I have been at this for hours now. Pulling my hair out.

I have a standard php page that has an onunload event on it. The
onunload event sends a xmlhttprequest that triggers a database update,
for simplicity lets say it updates a table:
UPDATE testtable SET testfield = '".$_GET['rand']."' where id = 1

This works just fine.

Then the next page you navigate to is supposed to call the testfield
from the database and it should display the newly updated value. It is
not. If you refresh the next page again you now get the correct newly
inserted value.

Does the onunload event prefetch the next page prior to executing the
onunload function???

Anyone have any ideas?? I cant find anything in the archives about
this.

Thanks.


My guess is that the SELECT statement from the second page is received
a few milliseconds after the UPDATE statement.

You could loop the SELECT until it gets 1 result, (also limit it to
like 20 tries or something so it doesn't go forever)

Feb 1 '06 #2

P: n/a
i have an alert statement after the update on the initial page so that
is not the issue as i can see that the value is being updated
correctly. but the next page still has the old value.

it is very strange.

Feb 1 '06 #3

P: n/a

tg****@gmail.com wrote:
i have an alert statement after the update on the initial page so that
is not the issue as i can see that the value is being updated
correctly. but the next page still has the old value.

it is very strange.


Is there only one link that goes to the next page, or can a differant
function be run when x is clicked, then have the function forward the
person to the next page after the XMLRequest Success code is returned?
If this works, then it has something to do with the onunload event.

Feb 1 '06 #4

P: n/a
I figured out the issue. when onunload is executed the browser has
already called the new requested page (it has not rendered it yet - but
it has put it in its memory). therefore the value is still the old
one.

I guess we are back to the drawing board.

Feb 1 '06 #5

P: n/a
tg****@gmail.com said the following on 2/1/2006 5:54 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
I figured out the issue.
Would the issue be you not quoting what you are replying to?

when onunload is executed the browser has
already called the new requested page (it has not rendered it yet - but
it has put it in its memory). therefore the value is still the old
one.

I guess we are back to the drawing board.

onbeforeunload can be your friend.
onclick can also.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

Feb 1 '06 #6

P: n/a
Thank you you are a genious. onbeforeunload. Why isnt this function
talked about more. This works EXACTLY as I want it to.

Thank you!!!

Randy Webb wrote:
tg****@gmail.com said the following on 2/1/2006 5:54 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
I figured out the issue.


Would the issue be you not quoting what you are replying to?

when onunload is executed the browser has
already called the new requested page (it has not rendered it yet - but
it has put it in its memory). therefore the value is still the old
one.

I guess we are back to the drawing board.

onbeforeunload can be your friend.
onclick can also.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Feb 3 '06 #7

P: n/a
tg****@gmail.com said the following on 2/3/2006 2:25 AM:
Thank you you are a genious. onbeforeunload. Why isnt this function
talked about more. This works EXACTLY as I want it to.


Please don't top-post either. It makes following a conversation difficult.

onbeforeunload isn't talked about a lot because of support, or lack of,
for it in different browsers.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.