0

I have a web app where I need to call a php function with parameters when the user navigates away from the page. This functionality doesn't need to be too sophisticated, simply when the user clicks another link on the page or goes to a different URL should suffice. Needed for a CRUD app to release the record lock which is executed via the function call (sproc wrapper).

php code

if (isset($_GET['modify'])) {
    ....
} else {
    // Putting the function here causes excessive calls
}

wrapper

function record_lock($row_id, $username, $end_lock){
    $stmt = mssql_init('spRecordLocks');
    mssql_bind($stmt, '@RowId', $row_id, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@Username', $username, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@EndLock', $end_lock, SQLVARCHAR, false, false, 50);

    $result = mssql_execute($stmt);
    $array = return_array($result, $stmt);

    return $array;
}

After researching, seems I need to make an ajax call though I am unsure of the implementation. Do I create a window.onunload = func.. Hoping someone can point me in the right direction. Thanks.

Zhorov
  • 28,486
  • 6
  • 27
  • 52
wonderstruck80
  • 348
  • 2
  • 13

1 Answers1

-1

use unload event on window to send ajax request

$(window).unload(function()
{
  $.ajax(...);
});
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
  • seems that i can only bind unload to window. possible to know when a user leaves a form on a page? that is the specific event i'm trying to capture. – wonderstruck80 Aug 16 '17 at 05:10