PHP Stop Loop on Button Click

I have a LOOP which runs for around 100 times. There is an echo statement in every loop.I am using ob_flush() and flush() for displaying the echo as it is processing. what I need is that when I click on a button during the displaying of echo I want the loop to exit. i.e I want break; to execute on click. Any ideas ?

The only kludgy way I could think of would be to have the button “set” something which the long-running task could periodically check, similar to a semaphore system in multitasking, though I don’t think the LRT can be made to react automatically.

So I’m thinking that the button would cause a file to be created, or a specific entry in a table that exists just for the job, and the loop would check to see if the file exists, or the table entry is present, each time it goes around the loop - if it is, then it exits the loop.

Perhaps someone with more PHP knowledge will say if it’s possible to interact with a running PHP script without doing something like the above.

ok I will try that

You can’t do that with php. Everything in the server-side loop is going to be sent to browser before the user even sees the page. You will need a combination of AJAX and/or web sockets interacting with server-side code to achieve that.

Sounds reasonable to me. OP did suggest that by using flush they were getting the php code to update the browser window during the loop progress, and maybe the button would be in another frame or something similar. As I said, kludgy.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.