Global php variable doesn't affect output of function [duplicate] - javascript

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; ?>

Related

PHP variable can be assign to javascript variable but javascript variable can't assign to php variable [duplicate]

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?

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.

Pass a value from javascript to php [duplicate]

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'];

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