user1687621
user1687621

Reputation: 433

Run PHP script in background using jQuery

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."

Upvotes: 0

Views: 3955

Answers (2)

Larry Lane
Larry Lane

Reputation: 2191

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>

Upvotes: 0

GrafiCode
GrafiCode

Reputation: 3374

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!!" );
  })
});

Upvotes: 1

Related Questions