Calling a javascript function while coding in PHP [duplicate] - javascript

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?

Related

variable in php to variable in javascript [duplicate]

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>

JavaScript call PHP function same value alwalys [duplicate]

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.

How to echo a JS variable to php? [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
How to convert Decimal to Double in C#?
(14 answers)
Closed 4 years ago.
Is that possible to pass a JS variable to PHP ?
My code is :
document.getElementById('designation_' + i).value = '<?php echo $list[HERE]; ?>';
where I is in a for loop.
I want to echo the $list array in PHP with the index I of my JS (instead of HERE).
First, is that possible? If yes, how?
You can not pass the JavaScript variable up to PHP to index the $list array. What you can do is to pass the whole array down to JavaScript as JSON encoded and then index into that.
See: Convert php array to Javascript

How to put this JS variable into PHP variable? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I would like to know how to make use of the variable hash in a PHP variable
var hash = md5(a);
Here is the equivalent in PHP:
$hash = md5($a);

Get Yii user state variables in javascript [duplicate]

This question already has answers here:
In Yii, pass PHP variables to JavaScript
(6 answers)
Closed 8 years ago.
I am using Yii::app()->user->setState to set some user's session variables.
Is there a way to get the values of them from within JavaScript?
Something like this perhaps? You have to echo it to the JavaScript.
<script>
var myVariable = "<?= Yii::app()->user->getState('myVariable', '') ?>";
alert(myVariable);
</script>

Categories