Running Two Activities on submit button


#1

I have a Submit button (Materialize Framework) which should perform two task:

  1. execute a python Query

  2. Download an excel file which is present in my folder path: C:\wamp64\www\Project\

Previously, I did try code mentioned below, which is downloading the excel:

<div class="button-container">
     <button class="btn waves-effect waves-light btn-small"
             type="submit" name="action" style="background-color: #424242; ">
        <a href="Format 1.xlsx" target="_new" download="Format 1.xlsx">
           <strong>Submit</strong>
        </a>  
        <i class="material-icons right">file_download</i>
     </button>
              
</div>

I tried to execute two task at the same time.
The following is the code which I tried and it is executing the python script but not downloading the excel:

<?php
    if(isset($_POST['update'])){
      echo "I am working";

      exec(
          '"C:\Users\Sonal Mulani\AppData\Local\Programs\Python\Python36\python.exe"
            C:\wamp64\www\BillingPortalNew\Pivot1.py'
         );
      echo "Script exec";

    }
    ?>




<div class="button-container">
     <form name="update" method="post" >
        <button class="btn waves-effect waves-light btn-small"
             type="submit" name="update" style="background-color: #424242; ">
           <a href="Format 1.xlsx" target="_new" download="Format 1.xlsx" id="abc"><strong>Submit</strong>
           </a>  
           <i class="material-icons right">
             file_download
           </i>
         </button>
     </form>
</div>

For downloading the excel I tried by assigning an id=abc to tag and calling it at the end:

     </body>

     <script>
     jQuery(function(){
        jQuery('#abc').click();
     });
     </script>

     </html>

Is there a solution to download the excel as well as executing the python script?


#2

Well, this can be tricky. Let’s explain PHP, PY and JQuery running all together before a solution.

PHP runs SERVER-SIDE only.
PY if installed on the server and run from the server would be SERVER-SIDE only.
HTML, Javascript, CSS and JQuery all run CLIENT-SIDE only.

So, if you use PHP to run a script it runs on the server. If it calls a PY script on the server, it is server side, too. Therefore the JQuery on the client is not aware that the server is completed all of it’s tasks.

Your current JQuery code executes on the browser as soon as you click the button. I am assuming that the PY script might be setting up the PDF file. ( ? ) If not, you can fix it by adding download code to the JQuery code, like this:

$('#abc').click(function(e) {
    e.preventDefault();    //stop the browser from following
    window.location.href = 'path-to-folder/file-name-to-be-downloaded.xlsx';
});

This means that you can not do it in your current way. You either have to have the SERVER-SIDE code create a button for downloading the file once the PHP and PY scripts are completed and display this button so the user can press it to download the file. OR, just have the PHP script force a download to start once the PY script is completed. You can make PHP start the download for you and then continue onto the display of the client-side data. To do that, you would need to add some code to the PHP file to start the download. Here is a way to make the PHP file download a PDF file after creating it. (If this is what you need! Not sure by your question!)

// The Excel sheet is saved to the file, now download it…
header(‘Content-type: application/xlsx’);
header(‘Content-Disposition: attachment; filename="’ . basename($excel_filename) . ‘"’);
header(‘Content-Transfer-Encoding: binary’);
readfile($excel_filename);

This is based on having the file name in the $excel_filename variable… You need to adjust it as needed…

Hope this helps!