Skip to content Skip to sidebar Skip to footer

AJAX Wont Call Its PHP Script While Another PHP Script Is Running

Scenario: I have two PHP scripts to be called simultaneously: First script will run several minutes (PHP based file download), depending on downloaded file size Second PHP script

Solution 1:

The first script it's not send through ajax:

 // run long running (later Download) PHP script
  console.log("Calling: PHP/fileDownload.php");
  window.location.href = 'PHP/fileDownload.php';

You simply redirect the user to another page, and because you have download headers in php, the file is downloaded in the same page.

You can easily achieve your scope through an iframe. You set the source of that iframe : 'PHP/fileDownload.php' and then simply call your ajax download checker.

Short example:

<iframe src="PHP/fileDownload.php">

<script>
        window.setTimeout(function(){startDownloadMonitoring()}, 1000);
        window.setTimeout(function(){startDownloadMonitoring()}, 3000);
        window.setTimeout(function(){startDownloadMonitoring()}, 5000);
        window.setTimeout(function(){startDownloadMonitoring()}, 7000);
        window.setTimeout(function(){startDownloadMonitoring()}, 9000);
        // .... blah blah
</script>

Solution 2:

When you call

    window.location.href = 'PHP/fileDownload.php';

the script execution stops (not immediately, see https://stackoverflow.com/a/2536815/2806497).

Are you sure ajax calls to fileDownloadStatus.php are executed ?

A solution would be to call the fileDownloadStatus.php file by an ajax asynchronous call, or maybe to load it into an iframe you put in your page.

Hope that helps.


Post a Comment for "AJAX Wont Call Its PHP Script While Another PHP Script Is Running"