1

I'm trying to execute my javascript functions, that have PHP code inside, when I click on a button but once I load the page on browser all javascript functions execute without clicking any button. When I try to press a button and execute a function nothing happens. My code is:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width" />
    <title> LED </title>
    <script>
        function exec_vermelho(){
            <?php exec( "sudo python led.py 100 0 0" ); ?>
        }
        function exec_verde(){
            <?php exec( "sudo python led.py 0 100 0" ); ?>
        }
        function exec_azul(){
            <?php exec( "sudo python led.py 0 0 100" ); ?>
        }
        function exec_amarelo(){
            <?php exec( "sudo python led.py 100 100 0" ); ?>
        }
        function exec_ciano(){
            <?php exec( "sudo python led.py 0 100 100" ); ?>
        }
        function exec_magenta(){
            <?php exec( "sudo python led.py 100 0 100" ); ?>
        }
        function exec_branco(){
            <?php exec( "sudo python led.py 100 100 100" ); ?>
        }
    </script>
</head>
<body>
    <input type="button" name="botao_vermelho" value="Vermelho" onclick="exec_vermelho()">
    <input type="button" name="botao_verde" value="Verde" onclick="exec_verde()">
    <input type="button" name="botao_azul" value="Azul" onclick="exec_azul()">
    <input type="button" name="botao_amarelo" value="Amarelo" onclick="exec_amarelo()">
    <input type="button" name="botao_ciano" value="Ciano" onclick="exec_ciano()">
    <input type="button" name="botao_magenta" value="Magenta" onclick="exec_magenta()">
    <input type="button" name="botao_branco" value="Branco" onclick="exec_branco()">
</body>
</html>

Does anyone know why all PHP on functions execute once I load the page and then nothing happens when I click on a button? Do I need to change anything on the buttons or javascript?

Thank you.

Leonardo Burrone
  • 339
  • 2
  • 5
  • 13

2 Answers2

2

PHP is server side code and will have executed all the code at the point you have loaded the page. Take a look at your source code in the browser and you will see that there is no javascript code inside your functions - You might want to look into using XMLHttpRequest or Fetch

Endless
  • 34,080
  • 13
  • 108
  • 131
-1

You can't make it like this, because PHP interprets your code before javascript. So it doesn't make sense to call a PHP function by Javascript.

You need to make an AJAX call to a php script, which runs the python script asynchronously.

Like:

// HTML
<input type="button" name="botao_vermelho" value="Vermelho">

// JS
$('input[type="button"]').click(function(event) {
     event.preventDefault();

     $.ajax({
         url: '/phpscript.php?button=' + $(this).attr('name'),
     }).done(function(data){
         console.log(data);
     });
});

And now just make a php script which runs your python executions. To know which button was clicked, i suppose the best way is to give the name attribute from each button to the php script.

Matt Backslash
  • 764
  • 1
  • 8
  • 20