0

I'm trying to call a php function, that run a shell command, inside my JavaScript on same page, it is supposed to return different value on each call but it call many times and use just the first return

PHP code:

<?php
    $consulta = 'snmpget.exe -r:'.$ip.' -c:'.$dominio.' -o:'.$oid.'';

    function exeSnmp ($consulta){
        $arr = explode('Value=', shell_exec($consulta));
        return preg_replace( "/\r|\n/", "", $arr[1] );      
    }   
?>

And my JS code:

setInterval(function() {
    var snmp = <?php echo json_encode(exeSnmp($consulta), JSON_HEX_TAG); ?>;
    //doing something with snmp var
}, 1000);

When I run this code, I get always the same value from call exeSnmp() function, but its change on my system.

Thanks in advance.

Daniel Beltrami
  • 756
  • 9
  • 22
  • 3
    This question has been asked and answered **many** times. PHP runs on the server-side and renders all the content **before** it gets to your browser. Once it gets there, the JavaScript is executed. If you view the page's rendered HTML source, you would see what's happening – Phil Jul 30 '18 at 00:21
  • 1
    Like `@Phil` said. Use AJAX. – StackSlave Jul 30 '18 at 00:22
  • I'll try, thank you! – Daniel Beltrami Jul 30 '18 at 00:24

0 Answers0