variable in php to variable in javascript [duplicate] - javascript

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>

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?

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

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>

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

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

Categories