使用jQuery在后台运行PHP脚本

Run PHP script in background using jQuery

本文关键字:PHP 脚本 运行 后台 jQuery 使用      更新时间:2023-09-26

PHP需要一些时间在后台运行。我只想让我的代码显示"Command executed"。请等待。"-这将是用户从他们的终端看到的所有内容。我的。php脚本中的其他所有内容都将在后台完成。

我正在考虑以某种方式用jQuery执行它,并让它停止加载脚本执行的帖子。然后使用ignore_user_abort()

下面是我打算使用的jQuery代码:

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

我唯一缺少的是让它在最初被调用后停止调用,然后我需要将它附加到一个div上,说"邀请待定"。

几乎与jquery的站点示例相同:

alert( "initializing..." ); 
var jqxhr = $.post( "myPHP.php", function(data) {
  alert( "working..." );
})
  .done(function() {
    alert( "finished!" );
  })
  .fail(function() {
    alert( "error!!" );
  })
});

将以下行放入myPHP.php文件中。

  echo "Command executed. Invites pending."; 

JavaScript代码:

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:

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