0

I am trying to run a python script or a batch file to create a sample file and send it over the mail when I click a button in a HTML page.

Below is my batch file(on running it creates a new file with 'sample text' inside)

// **foo.bat**
echo sample text > MyFile.txt

As I am using JavaScript, I need the above batch file to run in the background on a button click. So I tried to use iframe like below but the contents of batch file got displayed rather than running.

<html>
  <body>
    <iframe id="iframeId" width="1" height="1"></iframe>
    <input value="Run batch" onClick="RunBatch();"
  </body>
  <script>
     function RunBatch(){
        document.getElementById('iframeId').src="foo.bat";
     }
   </script>
</html>

I referred How to run .exe file or .bat file based on button click event using Javascript- duplicate thread but by using it, we should run as a HTA application which again behaves similar to a batch file.

Could anyone tell me a way to run the python script or a batch file in background on a HTML button click?

Onera
  • 687
  • 3
  • 14
  • 34
  • You need a web server to do that (and it needs to run on the computer where you want to run the script). The idea is that clicking the button sends a request to the server (something like `fetch('/foo.php')`) and when the server executes `foo.php` they call a system command (using [exec](https://www.php.net/manual/en/function.exec.php)) –  Jun 03 '20 at 06:59
  • You mean to say batch file will be placed in the server and we need to call it like an hosted url in the server only, if so will it run by url only or should I setup the environment? – Onera Jun 03 '20 at 07:03
  • To be clear, the server needs to be installed on the machine where you want the script to run, not the other way around. Say you have a new Macbook Pro, a $10,000 dedicated server and a 30 year old PC running Win 3.11. If you want that script to run on the old PC, you need to install the web server on the old PC. –  Jun 03 '20 at 07:07
  • 2
    Does this answer your question? [How to run Python script through HTML button?](https://stackoverflow.com/questions/52163199/how-to-run-python-script-through-html-button) – Abhishek J Jun 03 '20 at 07:27

0 Answers0