Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP / Ajax cancel a process running in a for-each loop

Posted on 2010-01-05
8
Medium Priority
?
375 Views
Last Modified: 2012-06-27
I have a PHP process which, when started iterates through a bunch of orders in a MySQL table using for-each, and calls a SOAP function to update the order from a remote server.

The process could take a few minutes, so I send updates to the browser using Ajax Prototype.Updater.  I need a way for the user to cancel the process mid-run.

I though about creating a session variable on a submit button, and in each run of the for-each loop checking if the session had been set, but it seems that updates from the browser, inlcuding pressing the cancel button,  are only sent after the process has completed, which is too late!

What's the best way of doing this?

Many thanks.
0
Comment
Question by:Ross Edwards
  • 4
  • 4
8 Comments
 
LVL 11

Expert Comment

by:VanHackman
ID: 26182015

You can do a Step By Step Iteration.

I mean, if your bunch of orders in a MySQL table have 100 items, process it in groups of 10, and store the array index of the next order to process in a session variable.

You start to process the orders, process the first 10, and stop it, send the page to the browser including the CANCEL button and a timer that call the same "ProcessScript.php" after a few seconds, in this period the user can see the progress of the process (you can print a progress bar or something) and cancel the process too, clicking on the CANCEL button, if the user don't take any action, the timer expire and call the "ProcessScript.php" witch look for the "CANCEL" variable in session if that variable is not set, it process the next 10 orders, starting on the index saved in session,  and repeat the process described.



0
 
LVL 1

Author Comment

by:Ross Edwards
ID: 26182038
Nice idea, but I would rather process the whole batch without introducing delays.  I may have to use your suggestion as a last resort but I am currently looking at getting the client side to cancel the Ajax requests...
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26182146

Ok, In this case, what about using Iframes?...

Is a less elegant solution, but if you put two Iframes in your page, you can print in one the CANCEL button, and leave the other one for the "ProcessScript.php", in this scenario the user can click on the CANCEL button in any time that he wants, and set the CANCEL variable in session, while the "ProcessScript.php" run in a parallel way, looking for the state of the CANCEL variable for stop itself. =P
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:Ross Edwards
ID: 26183984
Tried iframes, but same problem, iframe content does not get processed by the browser until the page has finished processing all iframes on it!
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 26185628

"Tried iframes, but same problem, iframe content does not get processed by the browser until the page has finished processing all iframes on it!"

Mmmm... I don't believe it... Because I have implemented it before, to display a dynamic progress bar only with Php and Css, to show the progress of a very loooong process (around 1 hour with 40 minutes). Probably the problem is the script that you show in the Iframe, because for my progress bar I had to implement some functions to handle the output buffer. =P

I mean: ob_start(), ob_flush(),  ob_clean() in every iteration...
0
 
LVL 1

Author Comment

by:Ross Edwards
ID: 26186293
Maybe would work in your situation but I am using Ajax Javascript calls to update a DIV element as the PHP process works.  This complication may affect how things work - I am not sure?
0
 
LVL 11

Accepted Solution

by:
VanHackman earned 2000 total points
ID: 26186438

I write a full code example for you:

index.php     -> The page with the Iframe.
LongProcess -> The page with the long process... =P
cancel.php     -> The page that set the Cancellation Flag.
cancel.txt      -> The file that acts like Cancellation Flag.

As you already read, I am using a file like "Cancellation Flag", this is not the best way!!! but, here is just to demonstrative proposes so, is OK...

How it works?

I show the main page with some text, and insert a iframe with the LongProcess script that is showing a progress bar that is being updated every 4 seconds, I use Sleep() to simulate a process delay, in the main page you will see a CANCEL button, if you click it, I use Jquery to call the cancel.php script, it create the cancel.txt file (set up the cancellation flag) and when the LongProcess script see the cancellation flag up,  stop the process and delete the cancel.txt file (set the flag down)

I try it and it works fine.

So I hope that this help you to get your own cancellation mechanism.
And don't forget give me some points if it helps you... ;)

Bye!

~VanHackman
index.php
LongProcess.php
cancel.php
0
 
LVL 1

Author Closing Comment

by:Ross Edwards
ID: 31673000
Thanks for the ideas - I have used some of your suggestions to resolve the problem.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question