Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: pause php loop using js

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,429
    Thanks
    104
    Thanked 117 Times in 115 Posts

    Default pause php loop using js

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Would this work?

    Code:
    if (confirm('Yes or No?')) {
       continue;
    }
    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,429
    Thanks
    104
    Thanked 117 Times in 115 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I'm not great with js either James but will something like this help?

    Code:
    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/";
      }
    }
    Code:
    <a href="javascript:confirmAction('http://ebay.com/')">EBAY</a>
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <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;
    }
    </script>
    </head>
    <body>
    <input type="text" id="test"><br>
    <input type="button" value="Go" onclick="atest();">
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,429
    Thanks
    104
    Thanked 117 Times in 115 Posts

    Default

    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.
    Last edited by james438; 06-18-2011 at 04:58 AM. Reason: typo
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Last edited by djr33; 06-18-2011 at 04:07 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    http://www.dynamicdrive.com/dynamici...photoalbum.htm

    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,429
    Thanks
    104
    Thanked 117 Times in 115 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 Code:
    <?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>
    <script>
       var _break = confirm("do you want to stop the loop?");
       $.post(
           'pseudoloop.php'
           ,{ "break":_break }
           ,function(response){ $("#response").html(response); }
       );
    </script>
    PHP Code:
    <?php // psuedoloop.php

    session_start();
    if(
    $_POST['break'] == "true"){ print "broken!"; }
    else{
        
    $_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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •