Note that there are some explanatory texts on larger screens.

plurals
  1. PORun ajax page in background (Android)
    primarykey
    data
    text
    <p>I'm trying to process database inserts in the background by calling a ajax request on page load. The problem I'm facing is that even if move from the page (the page on which the ajax request is made), it is still waiting for the process in the ajax page to complete.</p> <p>I do not want anything to be output from the ajax page, just start and keep running until all the insert statements are complete. After researching on this subject, I found some solutions but they are not working in my case. I'm using the below code:</p> <h2>index.php</h2> <pre><code>&lt;?php ignore_user_abort(true); ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="jquery.js"&gt;&lt;/script&gt; &lt;script&gt; $(function(){ $.ajax({ url:'ajax-page.php', success:function(ret) { // of no consequence as I'm not interested in output } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="somepage.php"&gt;Click Me&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>ajax-page.php</h2> <pre><code>ob_end_clean(); header("Connection: close"); ignore_user_abort(true); ob_start(); echo (' '); $size = ob_get_length(); header("Content-Length: $size"); ob_end_flush(); flush(); if(session_id())session_write_close(); // background process to start here for($i=1; $i&lt;=10;$i++) { // perform insert query here sleep(1); } </code></pre> <p>Now if I click <strong>Click Me</strong>, the navigation is waiting till the loop (the one in ajax page) has finished executing. Once all 10 records are inserted, the navigation proceeds.</p> <p>AFAIK, I think I'm using all the suggested tricks via <code>ignore_user_abort()</code>, <code>flush()</code>, <code>session_write_close()</code> etc. Am I missing anything else?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload