Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/Ajax Polling Progress
    primarykey
    data
    text
    <p>I am attempting to implement a live polling of a php script loop with no luck thus far in my attempts. Here is what I have thus far:</p> <p>On form submit:</p> <pre><code>$.ajax({ data: $(this).serialize(), success: showResponse, url: 'process.php', type: 'post' }); function showResponse(){ $.ajax({ type: "GET", url: "progress.php", cache: false, success: function(data) { var response = $.parseJSON(data); if (response.processing === true) { console.log("Current Item: " + response.currentItem + "Total Items: " + response.totalItems + "Percent Complete: " + response.percentComplete); setTimeout(checkProgress, 1000); }); } </code></pre> <p>In the process.php script:</p> <pre><code>session_start(); echo json_encode(array("processing" =&gt; true)); $totalItems = 10000000; $_SESSION['totalItems'] = $totalItems; $_SESSION['processing'] = true; $_SESSION['error'] = false; for ($i=0; $i &lt;= $totalItems; $i++) { $_SESSION['currentItem'] = $i; $_SESSION['percentComplete'] = round(($i / $totalItems * 100)); } </code></pre> <p>In the progress php script:</p> <pre><code>session_start(); echo json_encode(array( "processing" =&gt; $_SESSION['processing'], "error" =&gt; $_SESSION['error'], "currentItem" =&gt; $_SESSION['currentItem'], "totalItems" =&gt; $_SESSION['totalItems'], "percentComplete" =&gt; $_SESSION['percentComplete'] ) ); </code></pre> <p>Not sure where I am going wrong here but all it does is loop once it hits 100% complete. Any suggestions would be greatly appreciated!</p> <p><em>EDIT</em> I changed the above to using apc in the process.php:</p> <pre><code>apc_store('totalItems', $totalItems); apc_store('processing', true); apc_store('error', false); apc_store('currentItem', $i); apc_store('percentComplete', round(($i / $totalItems * 100))); </code></pre> <p>And within the progress.php:</p> <pre><code>echo json_encode(array( "processing" =&gt; apc_fetch('processing'), "error" =&gt; apc_fetch('error'), "currentItem" =&gt; apc_fetch('currentItem'), "totalItems" =&gt; apc_fetch('totalItems'), "percentComplete" =&gt; apc_fetch('percentComplete') ) ); </code></pre> <p>Still doesn't work properly the way I am wanting it to work, am I doing something incorrectly? It only shows false values until the script completes and shows 100% just like the session use was doing before. Any ideas?</p>
    singulars
    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.
 

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