This question already has answers here:
Call php function from JavaScript
(5 answers)
How can I call PHP functions by JavaScript?
(13 answers)
Closed 4 years ago.
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.
Related
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 3 years ago.
If we have a variable in php which holds a value then we can assign easily this value to js variable but the vice versa is possible?
For Assign php variable to js variable we use
<?php $r = 1; ?>
<script>
var s = "<?php echo $r; ?>";
</script>
But for assign js variable to php variable its not works
<script>
var s = 1;
</script>
<?php $r = echo "<script> s </script>"; ?>'
Not working
Generally, PHP is server side while Javascript is client side. You can do it by creating a browser cookie using Javascript and then use PHP to read that cookie.
Anyway, why do you want to do that?
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
I want to take my variable in php, and put in a variable in javascript
there is my code in php :
$SQL=$pdo->query("SELECT SUM(nbdeces) as totalnbdeces FROM nombre_deces");
$data = $SQL->fetch();
$sommeanswered = $data['totalnbdeces'];
I want to stock the variable "$sommeanswered" in a variable javascript
You can do that like
<script>
var val = "<?php echo $sommeanswered; ?>";
</script>
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 6 years ago.
I want to send the value of address to a new PHP page
<script>
var address = place.formatted_address;
document.getElementById('af').innerHTML = address;
</script>
PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:
<script type="text/javascript">
var foo = '<?php echo $foo ?>';
</script>
To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});
On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am trying to get a variable from my javascript function while I'm coding in PHP.
my javascript which is included in the page:
function subAmt(empid, subid){
return 4;
}
my attempt to call that function:
$amt = echo "subAmt(3,5);" ;
You need the script tags I think?
echo '<script type="text/javascript">'
, 'subAmt(3,5);'
, '</script>'
;
Please refer to this question:
How to call a JavaScript function from PHP?
This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Closed 8 years ago.
I am trying to implement this example on my website: http://jsfiddle.net/eDeQr/1/
The problem is that I need the $cp1 = 4;variable to be global and not inside that function. As soon as I do that the function doesn't give any output in the second field.
This is what I want it to look like:
<script type="text/javascript">
function calc() {
var textValue1 = document.getElementById('input1').value;
var textValue2 = $cp1;
document.getElementById('output').value = textValue1 * textValue2;
};
</script>
The $cp1 variable should be global.
You're mixing JavaScript and PHP. You actually have to echo out the PHP variable inside of your JavaScript. Otherwise the JavaScript is looking for a JavaScript variable named $cpl which is undefined.
var textValue2 = <?php echo $cp1; ?>