如果浏览器关闭,则解锁表

If someone wants to edit an entry in a table, I put a column "Locked" to true. If the user closed the browser / tab, how can I do it best so that the value is written by "Locked" back to false?

Is that possible with ajax? This can be seen when the user closes the browser / tab, then that ajax event fired?

Or is there a better and more reliable method? I work with laravel 4 und MSSQL.

This is very dangerous and highly unreliable, so if you need those rows unlocked, you better put a timeout on them too (a locked_at column), index the column and search for locked rows to unlock the timeouted ones. But...

You can hook on browser unload javascript event to help you with that:

window.onbeforeunload = function(event) {
    var s;
    event = event || window.event;
    if (requestsPending > 0) {
        s = "Your most recent changes are still being saved. " +
            "If you close the window now, they may not be saved.";
        event.returnValue = s;
        return s;
    }
}

Tricky to do reliably, a couple of methods:

Use onbeforeunload as Antonio suggests, you could do an ajax request to release the lock?

Lock the record for x minutes on edit, then renew the lock via a polling ajax request just before the lock expires.