0

I'm writing a plugin for wordpress, in page1.php I have a form with a radio button and a submit button. The action of the form is myscript.php, How can I know when myscript.php has finished running?

page1.php:

echo '<form id="download_form" method="POST" action="myscript.php?download_the_excel=1">
      <ol>
        <li><input type="radio" name="input1" value="value1">radio_1</span></li>';
        <li><input type="radio" name="input1" value="value2">radio_2</span></li>';
      </ol>
      <input id="export_button" type="submit" name="submit_button" value="Export">';

And then we have myscript.php which is doing some calculation and finishes the script with some headers() with an Excel attachment:

<?php
if(isset($_GET['download_the_excel']) && isset($_POST['input1']) && is_admin()){

    $myData = some_huge_function();
    $objPHPExcel = $myData;
    header('Content-Type: application/vnd.ms-excel');   
    header('Content-Disposition: attachment;filename="filename.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
}

Now my problem is that when the user is on page1.php, and clicks the download button, I would like to put a Javascript loading(png image / loader / whatever..) while myscript.php is running, and remove it after. How can I know when the script has finished?

Note: I can do something like that in page1.php:

echo '<script>
        $("#spinner_loader").hide();
        $("#export_button").click(function(){
          $("#spinner_loader").show();
        });</script>';

When I do this, spinner_loader will never end (because page will not reload).

wp42
  • 158
  • 10

1 Answers1

0

You can simply use jQuery AJAX post method instead of traditional form submitting.

  1. https://api.jquery.com/jquery.post/
  2. https://api.jquery.com/submit/

UPD: Then you will need to create temporary downloadable file in order to let users download responded data.

"How to do that?" Here you go https://stackoverflow.com/a/23497960/701666

Elvin Haci
  • 3,503
  • 1
  • 17
  • 22
  • 4
    Not that simple: `header('Content-Disposition: attachment;filename="filename.xls"');`. – dfsq Nov 01 '17 at 08:21
  • @wp42, what is the main difficulty with using jQuery Post method for this case? If returned data can't raise download action, it is not a problem, with small tuning you can do it with iframe logic or with window.location.href – Elvin Haci Nov 01 '17 at 08:26
  • 2
    The main difficulty is that making POST request with AJAX like you are suggesting is not enough, and will not work for OP purposes (won't download attachment). – dfsq Nov 01 '17 at 08:27
  • 1
    Yes, it is not enough, because i just wrote a solution to answer the main question - "How to determine when a script was finished running?". Making AJAX data downloadable should not be more difficult than current question. f.e. Simply saving the data to temporary downloadable file, return its path as AJAX response, and then do what you want with that returned link. – Elvin Haci Nov 01 '17 at 08:31
  • @wp42, https://stackoverflow.com/questions/23497573/receiving-a-xls-file-using-ajax-jquery-post – Elvin Haci Nov 01 '17 at 08:40
  • 1
    That's correct approach. Generate file, store it in temp location, respond with file path. Create hidden iframe with src to this file path. Maybe if you could update you answer then it would be more complete, right now it's not very useful. But whatever. – dfsq Nov 01 '17 at 08:41