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.

3
  • 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 Commented Jul 30, 2018 at 0:21
  • 1
    Like @Phil said. Use AJAX. Commented Jul 30, 2018 at 0:22
  • I'll try, thank you! Commented Jul 30, 2018 at 0:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.