views:

30

answers:

1

Hi,

i'm trying to lock a row in a db-table when a user is editing the entry. So there's a field in the table lockthat I set 1 on page load with php.

Then I was trying to unlock the entry (set it 0) when the page is unloaded. This is my approach. It works fine in IE but not in Firefox, Chrome etc.... The window.onbeforeunload works in all browsers, I tested that. They just don't do the Request BUT if I simple put an alert after req.send(); it works in some browsers but not safari or chrome. So I tried putting something else after it just so that's there's other stuff to do after the request but it doesn't work.

function test() { var req = new Request({ url: 'inc/ajax/unlock_table.php?unlock_table=regswimmer&unlock_id=', }); req.send(); alert('bla'); // ONLY WORKS WITH THIS !?!?!? }

window.onbeforeunload = test;

i've already tried different ways to do the request but nothing seems to work. And the request itself works, just not in this constellation.

ANY help would be appreciated!

Thanks

+3  A: 

the request is asynchronous by default. this means it will fork it and not care of the complete, which may or may not come (have time to finish). by placing the alert there you ensure that there is sufficient time for the request to complete.

basically, you may be better off trying one of these things:

  1. add async: false to the request object options. this will ensure the request's completion before moving away.
  2. use an image instead like a tracking pixel.
  3. move over to method: "get" which is a bit faster as it does not contain extra headers and cookie info, may complete better (revert to this if async is delayed too much)

you can do the image like so (will also be $_GET)

new Element("img", {
    src: "inc/ajax/unlock_table.php?unlock_table=regswimmer&unlock_id=" + someid + "&seed=" + $random(0, 100000),
    styles: {
        display: "none"
    }
}).inject(document.body);

finally, use window.addEvent("beforeunload", test); or you may mess up mootools' internal garbage collection

Dimitar Christoff
thanks a lot, I added async: false and it's working now :-)
Roman
oh and I used window.addEvent('unload').
Roman
ahhhh, beforeunload didn't work in opera so....
Roman
feel free to approve the answer by clicking on the tickbox thing / up arrow :)
Dimitar Christoff
You'll at least get 1 upvote. :D I have one question: what's the big difference between an image and the Ajax Request?
Savageman
basically, none in this case. he just needs to SEND data to the server and is not waiting for a response. hence, ANY kind of http request will do to make the server unlock the DB, be that for as an image src or XHR.
Dimitar Christoff
hmm, i don't know how to vote for this :-/
Roman