View Full Version : pause php loop using js

06-16-2011, 12:52 AM
I am looking for a simple confirmation popup.

if "yes" resume script, if "no" continue. I mean continue in the php sense where it skips the rest of the loop and proceeds with the next as opposed to break, which exits out of the loop entirely.

06-16-2011, 05:20 AM
Would this work?

if (confirm('Yes or No?')) {

Continue, break, or whatever else you'd like.

Untested, and this is just a guess. But I don't see why it wouldn't work.

06-16-2011, 12:40 PM
It might, but I'm hoping for an example to see how it would work. I do not work with js very much, so I am having a little trouble seeing how this should look.

06-16-2011, 02:29 PM
I'm not great with js either James but will something like this help?

function confirmAction(goUrl) {
if (confirm("Are you sure you want to go to eBay?")) {
// do something on OK
document.location = goUrl;
} else {
// do something else on Cancel
// document.location = "http://google.com/";

<a href="javascript:confirmAction('http://ebay.com/')">EBAY</a>

06-16-2011, 02:31 PM
Do you have an actual javascript loop that you want to break out of? Here's a fairly simple example of that. It goes very fast though it still illustrates the concept:

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function atest(){
if(atest.running) return;
atest.running = true;
for(var i = 0; i < 11; ++i){
document.getElementById('test').value = i;
if(i === 5 && !confirm('i has reached 5, continue?')) break;
alert('The End');
atest.running = false;
<input type="text" id="test"><br>
<input type="button" value="Go" onclick="atest();">

06-16-2011, 08:55 PM
That is a lot closer to what I am looking for Beverly, but I am trying to break out of a php loop using a javascript confirmation popup.

jscheuer1, while it does not break out of a php loop I daresay that what you posted would actually work instead. I'll see if I can integrate it into my flatfile editor script later, possibly after I get home from work tonight.

The script is somewhat long for posting here, but there is an option to delete directories and I want people to be absolutely sure they want to delete the directory if they choose that option. Another place to use this is my image upload script, which uploads up to 5 image sat a time. If the image exists there should be a confirmation popup to see if the user (me) wants to replace the image. If yes then replace. If no then continue.

I changed the title to this thread for clarification.

06-17-2011, 08:58 PM
Ah, you're looking for a way to terminate the connection to the PHP if the user cancels? I don't think that's at all possible. You can stop loading the page. Is there a "cancel request" function in JS? (I don't know.)

PHP automatically stops processing once the user is no longer receiving-- but I don't know how immediate that is. It might run for another few miliseconds, enough to continue whatever action was "cancelled".

You could use Ajax to control all of it. Or you could reload the page multiple times if the image exists, or whatever is needed. To me, it sounds like you're getting to a level of complication where Ajax might be needed.

Remember, PHP executes and only AFTER is the HTML loaded. There's the slight exception that PHP can sometimes output some text before the whole page has been parsed. So if you could communicate in real time and there was enough of a pause it *might* work. But that would require communication from JS, which probably isn't even active until the page is fully loaded. And you can't amend the PHP request after it has been submitted.
Regardless even if in some strange way you could make this work, you couldn't then continue the script if the method you use is canceling. Since the PHP parser takes no input from the receiving browser, the very best you could do is work out canceling an operation if required.

Note that you can add a 5 second pause to PHP execution and perhaps attempt to make JS work that way, but I don't really see how. I *guess* you could attempt to use Ajax to set a session variable, and that session variable would be (I think) immediately available in the delayed script once it resumes..... complicated. It's an interesting question, but in reality probably not worth your time trying to figure it out (and possibly/probably finding no realistic solution).

Anyway, the simple answer is to do this before the form is submitted-- check if the filenames exist by preloading a list of existing filenames into JS and comparing then. Confirm at that point (or cancel and don't submit).

Or use Ajax.

06-17-2011, 11:25 PM
Is this for your own use as opposed to for people logged on to your site?

In any case, quite a few months back, more or less for my own amusement I worked out an adaptation of Dynamic Drive's PHP Photo Album script:


In it one can view the images automatically retrieved from a folder in thumbnail view. If you click on one you see it full size.

In my version (I hope I'm remembering all of these details correctly, some may be slightly different) the images all have their filenames below them in the display. If you click on the image, while seeing it full size you have the opportunity to accept or reject its deletion. If it is deleted, it's replaced in the display by a deleted icon/image and it's moved to a holding area - like a recycle bin. It's filename remains below the deleted icon. You may restore all (a button) or restore particular images by clicking on their spot in the original display which was replaced by an image/icon that has the word deleted on it. One may also restore manually by accessing the file system. There's a button to empty the holding are, like emptying the recycle bin - permanent deletion, a file recovery tool would be required to get it back after that.

Does this sound like what you're after?

06-18-2011, 04:58 AM
I see what you are saying djr33. I was forgetting some of the very basics of javascript and php, namely that javascript is strictly client side and php is strictly server side and the two do not interact all that well together; at least not in the way that I am thinking.

That being said Beverly's initial suggestion would work just fine for a confirmation popup when attempting to delete a directory.

window.location = "http://www.mysite.com?delete=yes";

With the other script where I am uploading 5 image files and 3 of which already exist it would be better to simply have a confirmation page informing me that I have 3 files already in the folder with the same names as the files I am trying to upload and list which ones they are and ask if I would like to overwrite them.

jscheuer1, your method sounds like it could be modified to do what I am looking for, but also looks like it would be more complicated when a simpler method would work.

06-18-2011, 06:01 AM
php cannot "receive" input from the user once it has started executing. In this instance, you need to break each step of the loop into its own php script execution, with javascript returning the break/continue directive. Ajax is the best way I can think of to do this.

if I understand your problem correctly, this might work:

<?php // pseudoloop_start.php
session_start(); ?>
<div id="response"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
var _break = confirm("do you want to stop the loop?");
,{ "break":_break }
,function(response){ $("#response").html(response); }

<?php // psuedoloop.php

if($_POST['break'] == "true"){ print "broken!"; }
$_SESSION['i'] = empty($_SESSION['i']) ? 1: $_SESSION['i'] + 1;
print 'pseudo-loop # '.$_SESSION['i'];

?>...but it would be difficult to integrate into a larger php script.

06-18-2011, 06:39 AM
thanks for the reply traq, but I think I have a pretty good idea of how I want to address this. javascript is nice, but not strictly needed and if I can do this using php, then I will do that instead since I am much more comfortable with php than javascript.

Thank you all for your responses. You have all given me several ideas on how to address something like this in the future if I decide to refine my script further.