javascript - How to make PHP execution halt until some action is taken - Stack Overflow

I have a PHP file executing, which inserts entries into the database, and when a condition is met (for

I have a PHP file executing, which inserts entries into the database, and when a condition is met (for example, $rows>500) i want to halt the execution, and two buttons to appear.

One with the value "Continue" to continue the execution of the script from where it stopped and one with "Cancel" to delete the entries that have been inserted until now, through the script.

The problem is that i cannot think of a way for stopping the execution, displaying the two buttons and then depending to my option an action to be taken.

Do I need some Javascript and AJAX?

Thanks in advance! Any help is appreciated!

I have a PHP file executing, which inserts entries into the database, and when a condition is met (for example, $rows>500) i want to halt the execution, and two buttons to appear.

One with the value "Continue" to continue the execution of the script from where it stopped and one with "Cancel" to delete the entries that have been inserted until now, through the script.

The problem is that i cannot think of a way for stopping the execution, displaying the two buttons and then depending to my option an action to be taken.

Do I need some Javascript and AJAX?

Thanks in advance! Any help is appreciated!

Share Improve this question asked Sep 25, 2012 at 7:57 Apostolos KouApostolos Kou 851 gold badge3 silver badges8 bronze badges 4
  • As a single PHP script is 100% server side you cannot have any user interaction. You'll need to split your server side ops into chunks and use AJAX to interact with it. – m90 Commented Sep 25, 2012 at 8:01
  • m90 can you give me an example please? thanks for your time – Apostolos Kou Commented Sep 25, 2012 at 8:03
  • See @khael's answer that describes pretty much what to do. – m90 Commented Sep 25, 2012 at 8:09
  • I doesn't necessarily needs AJAX... what is wrong with regular responses/page-rendering? – pedrofurla Commented Sep 25, 2012 at 8:09
Add a ment  | 

5 Answers 5

Reset to default 5

Have you tried using exit(); in the script? this exits the code, thus halting it. for the continue you could store the pos you were at in an session.

$i = 0;
while () {

     if ($rows > 500) {
             echo '<div id="continueAction" class="someButton">Continue</div>';
             echo '<div class="someButton">Cancel</div>';
             $_SESSION['pos'] = $i;
             exit();
     }
     // Store some value in DB here
     $i++;
}

Then with an ajax request you could start again at the spot you left off, using the stored 'pos' in your session..

EDIT: For ajax you will need a php script and a way to call the script from your button. I would use jquery for it. since you prob already have that on your site, you can just use:

 $('#continueAction').click(function(){
      $.get('ajax/test.php', function(data) {
         $('.result').html(data);
      });
 });

What this does is call the script test.php and brings back any data into a javascript variable called data.. and then it puts this data inside of the .result element.
Depending on your type of application you would have to mess around with this a bit to get your data where you need it. but this should get you into the right direction.

Hope this helped!

You must save the state of the application in a way or another so you can resume it, if you get to send some info to the client, like the form containing the two buttons, the php script will be way over, or if it will not, it will still require another request for submitting the form values ("continue" or "Cancel").

One way to do that is websockets and maybe node.js. Or a good IPC between two php scripts. But your best option is still AJAX.

Just make the script return a status code, as "not done", and make javascript show two buttons when this happens, save where you left in a session variable, echo "not done", or echo "{'status':'continue?'}"; and then a die();

And in javascript just ask the user what to do further.

Like m90 sais you can't by just using php.

Work with a transaction like structure, that can be rolled back. Make an ajax call, if condition is met. send somthing to the output, receive the responce, ask for user interaction. make a new call continue and mit the transacition or rollback.

Edit:

easy example of an ajax call using jQuery:

function runthescript(continue,rollback){
    $.ajax({
       type: "POST",
       url: "yourphpscript.php",
       data: {
         "isContinueing": (continue===true), 
         "isRollback": (rollback===true)
       },
       success: function(msg){
         if(msg === "calling for user interraction"){// replace this by Enum or something more performant this is just for illustration
              var answer = confirm("Want to continue?");
              if(answer){
                  runthescript(true, false);
              } else {
                  runthescript(false, true);
              }
         } else if(msg === "pleted"){// replace this by Enum or something more performant this is just for illustration
              alert('pleted');
         } else if(msg === "rolled back"){// replace this by Enum or something more performant this is just for illustration
              alert('rolled back');
         }
       }
     });
}
runthescript();

PHP example

<?php 
function checkStates(){
    if($rows>500){
         echo "calling for user interraction"; // replace this by Enum or something more performant this is just for illustration
         //exit(); //you can use exit if absolutely necessary, avoid if not needed.
    }
    if($finished_condition_is_met){
         echo "pleted";// replace this by Enum or something more performant this is just for illustration
         //exit(); //you can use exit if absolutely necessary, avoid if not needed.
    }
}

if($_POST['isContinueing'] === true){
    //run some continuing php


    checkStates();

} else if($_POST['isRollback'] !== true){

    //run some rolling back code   
    echo "rolled back";// replace this by Enum or something more performant this is just for illustration
    //exit(); //you can use exit if absolutely necessary, avoid if not needed.

} else {

    //run some starting php

    checkStates();

}

The simplest solution I can think of is to break the PHP script in two, and the "continue" button call the 2nd script that proceeds the putation.

If you test request parameters you can even do it in one script.

Ok, let me have a go at this one.

if($rows>500)
{
    echo '<div id="cancel">Cancel</div>';
    echo '<div id="continue">Continue</div>';
    exit();
}

then include jquery in your index page and have this:

$(document).ready(function() {
    $("#cancel").click(function() {

        var data_to_send = what you want to send

        $.ajax({
            type: "POST",  // might be too long to use GET
            url: "cancel.php", // script you want to carry out this action
            data: data_to_send,
            success: function(data) {
                $('#results').html('<p>Previously inserted rows have been deleted</p>');
                // data is just what cancel.php echos out
            }
        });
        return false;
    });

    $("#continue").click(function() {

        var data_to_send = what you want to send

        $.ajax({
                type: "POST",
                url: "continue.php",
                data: data_to_send,
                success: function(data) {
                    $('#results').html('<p>Continuing!</p>');
                }
        });
        return false;
    });
});

The results div can be an empty or hidden div that is used to output what's happening with the ajax requests or you can choose to append the data to an existing div.

Only critique I'd make is to swap the divs for actual buttons but it will work the same and I just prefer styling divs...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744298321a4567372.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信