How to cancel a PHP process, when ajax call is cancelled? How to cancel a PHP process, when ajax call is cancelled? ajax ajax

How to cancel a PHP process, when ajax call is cancelled?


Your server side php has to output something to detect the request is canceled. But as you are likely performing a lengthy sql query, you can't output anything.

But still you can save your connection id into a session, and kill the connection if it's detected:

  1. Open session
  2. Open sql connection
  3. If search_connection_id is set in session, kill the connection
  4. Find your connection id, save in session as search_connection_id
  5. Close the session - important, else the next request will be blocked in step 1
  6. Query database
  7. If connection is broken, it's another search killing you in step 3, exit
  8. Open session, remove search_connection_id
  9. Send response, close sql connection


Thank you all for your answers.

I redesigned my search queries like Anigel suggested, with an overwhelming result.

But i think the correct answers to my question are the answers of Marek and Harikrishnan Viswanathar. Both should do what i asked for.