1

I have a button on my webpage that executes a python script and am trying to create another one that executes javascript and then the python script.

However, I can't get it to do both. It will either send out the email or change the button.

<script type="text/javascript">

var alertTimerId = 0;

function alertTimerClickHandler ( )
{
  if ( document.getElementById("alertTimerButton").value == "RESTART" )
  {
    // Start the timer
    document.getElementById("alertTimerButton").value = "RESTARTING PLEASE WAIT......";
    alertTimerId = setTimeout ( "showAlert()", 3000 );
  }
  else
  {
    document.getElementById("alertTimerButton").value = "RESTART";
    clearTimeout ( alertTimerId );
  }
}

function showAlert ( )
{
  alert ( "process has been restarted" );
  document.getElementById("alertTimerButton").value = "RESTART";
}

</script>
<!-- <form name="update" method="post" >
<button onclick="alertTimerClickHandler()" name = "clickMe" value= "RESTART" id="alertTimerButton" > RESTART </button>
</form> -->
<form action=index.php method= "get">
<input type="button" name="clickMe" id="alertTimerButton" value= "RESTART" onclick="alertTimerClickHandler()" />
</form>
<?php
if (isset($_GET['clickMe']))
{
        exec("/anaconda3/bin/python /Users/restartemail.py \"$input_val\""); 
} else{

}
?>

my code that works is

<form name="update" method="post" >
<button name = "update" type="submit"> Send Update Email </button>

if (isset($_POST['update']))
    {
            exec("/anaconda3/bin/python /Users/sendemail.py \"$input_val\""); 
    }
j.baegs
  • 209
  • 2
  • 13
  • Whre does `$input_val` come from? – M. Eriksson Jul 25 '19 at 14:44
  • 1
    Also, the PHP-code will never be executed since you don't actually submit the form at any point. You might want to look into Ajax, if you want the PHP-script to run without the page reloads (like on an ordinary form submit) – M. Eriksson Jul 25 '19 at 14:46
  • i have added the code that works to the end of the question is there any way to transfer that to the above code – j.baegs Jul 25 '19 at 14:53
  • 1
    `$input_val` is vulnerable to command injection – Lawrence Cherone Jul 25 '19 at 14:57
  • 1
    The reason it works with ` – M. Eriksson Jul 25 '19 at 14:59
  • it's working now thankyou, the only issue is the alert isnt popping up – j.baegs Jul 25 '19 at 15:13
  • That's because the page will reload when you submit the form. This is why I mentioned Ajax. With Ajax, you can make a request in the background, without leaving/reloading the page. – M. Eriksson Jul 25 '19 at 15:24
  • 1
    Possible duplicate of [how to run Python script from php](https://stackoverflow.com/questions/49189730/how-to-run-python-script-from-php) – tartaruga_casco_mole Jul 25 '19 at 15:53

0 Answers0