title: Efficient and user-friendly way to present slow-loading results
body: <p>I have read <a href="https://stackoverflow.com/search?q=cancel%20post%20request%20jquery">many similar questions</a> concerning cancelling a POST request with jQuery, but none seem to be close to mine.</p>
  
  <p>I have your everyday form that has a PHP-page as an action:</p>
  
  <pre><code>&lt;form action="results.php"&gt;
    &lt;input name="my-input" type="text"&gt;
    &lt;input type="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Processing <code>results.php</code> on the server-side, based on the post information given in the form, takes a long time (30 seconds or even more and we expect an increase because our search space will increase as well in the coming weeks). We are accessing a Basex server (version 7.9, not upgradable) that contains all the data. A user-generated XPath code is submitted in a form, and the action url then sends the XPath code to the Basex server which returns the results. From a usability perspective, I already show a "loading" screen so users at least know that the results are being generated:</p>
  
  <pre><code>$("form").submit(function() {
    $("#overlay").show();
  });
  
  &lt;div id="overlay"&gt;&lt;p&gt;Results are being generated&lt;/p&gt;&lt;/div&gt;
  </code></pre>
  
  <p>However, I would also want to give users the option to press a button to cancel the request <em>and</em> cancel the request when a user closes the page. Note that in the former case (on button click) this also means that the user should stay on the same page, can edit their input, and immediately re-submit their request. It is paramount that when they cancel the request, they can also immediately resend it: the server should <em>really</em> abort, and not finish the query before being able to process a new query.</p>
  
  <p>I figured something like this:</p>
  
  <pre><code>$("form").submit(function() {
    $("#overlay").show();
  });
  $("#overlay button").click(abortRequest);
  $(window).unload(abortRequest);
  
  function abortRequest() {
    // abort correct request
  }
  
  &lt;div id="overlay"&gt;
    &lt;p&gt;Results are being generated&lt;/p&gt;
    &lt;button&gt;Cancel&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But as you can see, I am not entirely sure how to fill in <code>abortRequest</code> to make sure the post request is aborted, <em>and terminated</em>, so that a new query can be sent. Please fill in the blanks! Or would I need to <code>.preventDefault()</code> the form submission and instead do an ajax() call from jQuery?</p>
  
  <hr>
  
  <p>As I said I also want to stop the process server-side, and from what I read I need <code>exit()</code> for this. But how can I <code>exit</code> another PHP function? For example, let's say that in <code>results.php</code> I have a processing script and I need to exit that script, would I do something like this?</p>
  
  <pre><code>&lt;?php
    if (isset($_POST['my-input'])) {
      $input = $_POST['my-input'];
      function processData() {
        // A lot of processing
      }
      processData()
    }
  
    if (isset($_POST['terminate'])) {
      function terminateProcess() {
        // exit processData()
      }
    }
  </code></pre>
  
  <p>and then do a new ajax request when I need to terminate the process?</p>
  
  <pre><code>$("#overlay button").click(abortRequest);
  $(window).unload(abortRequest);
  
  function abortRequest() {
    $.ajax({
      url: 'results.php',
      data: {terminate: true},
      type: 'post',
      success: function() {alert("terminated");});
    });
  }
  </code></pre>
  
  <p>I did some more research and I found <a href="https://stackoverflow.com/questions/16524203/php-auto-kill-a-script-if-the-http-request-is-cancelled-closed">this answer</a>. It mentions connection_aborted() and also session_write_close() and I'm not entirely sure which is useful for me. I do use SESSION variables, but I don't need to write away values when the process is cancelled (though I would like to keep the SESSION variables active).</p>
  
  <p>Would this be the way? And if so, how do I make one PHP function terminate the other?</p>
  
  <hr>
  
  <p>I have also read into Websockets and it seems something that could work, but I don't like the hassle of setting up a Websocket server as this would require me to contact our IT guy who requires extensive testing on new packages. I'd rather keep it to PHP and JS, without third party libraries other than jQuery.</p>
  
  <p>Considering most comments and answers suggest that what I want is not possible, I am also interested to hear alternatives. The first thing that comes to mind is paged Ajax calls (similar to many web pages that serve search results, images, what-have-you in an infinite scroll). A user is served a page with the X first results (e.g. 20), and when they click a button "show next 20 results" those are shown are appended. This process can continue until all results are shown. Because it is useful for users to get all results, I will also provide a "download all results" option. This will then take very long as well, but at least users should be able to go through the first results on the page itself. (The download button should thus not disrupt the Ajax paged loads.) It's just an idea, but I hope it gives some of you some inspiration.</p>