0

So I have some PHP that takes some time that runs in the background. I just want my code to say "Command executed. Invites pending." - And that will be all that the user will see from their end. Everything else in my .php script will be done in the background.

I was thinking about somehow executing it with jQuery and have it stop loading the script mid-execute of the post. And then use ignore_user_abort()

Here is the jQuery code I plan on using:

jQuery.ajax({
    type: 'POST',
    data: { ' MyPostData'},
    dataType: 'html',
    url: 'myPHP.php',
    success: function (d) { /* commands */ }
});

The only thing I'm missing is having it stop the call after it's originally called upon, and then I need for it to append to a div saying "Invites Pending."

2
  • A suggestion for this is to look into actually making your long running processes run in the background. You could use something like beanstalkd (check pheanstalk for a php implementation of it), but you would need the ability to install beanstalkd on your server. A cron based solution might also work Commented Sep 29, 2015 at 22:27
  • Could you not just add async:true and update the wording in the div then you shouldn't need to do anything else. Commented Sep 29, 2015 at 22:59

2 Answers 2

1

Almost identical to jquery's site example:

alert( "initializing..." ); 

var jqxhr = $.post( "myPHP.php", function(data) {
  alert( "working..." );
})
  .done(function() {
    alert( "finished!" );
  })
  .fail(function() {
    alert( "error!!" );
  })
});
Sign up to request clarification or add additional context in comments.

Comments

0

Place the following line in your myPHP.php file.

  echo "Command executed. Invites pending."; 

JavaScript code:

jQuery.ajax({
 type: 'POST',
 data:$(this).serialize(),
 dataType: 'html',
 url: 'myPHP.php',
 success: function (d) {//begin success

    //add the pending message to a div on the page
    $("#result").html(d);

 }//end success
});

HTML needed on your page:

         <!-- results from the ajax function will be displayed here -->
         <div id = "result"></div>

2 Comments

Okay. Will that not keep the user waiting on the page to finish loading while it waits on the php?
I believe I may have misunderstood your question. Do you want to block the user from doing anything until the php script is finished running?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.