Get Yii user state variables in javascript [duplicate] - javascript

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>

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>

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

Calling Javascript variable inside javascript code [duplicate]

This question already has answers here:
How can I do string interpolation in JavaScript?
(21 answers)
Closed 6 years ago.
I have the following code
dt.ajax.url( 'test.php?status=' ).load();
I defined a variable
var status= 55
I wand to add status_2 in the link like this
dt.ajax.url( 'test.php?status=&status' ).load();
How can I do it ?
Try this. You need to concatenate the variable with url
dt.ajax.url('test.php?status='+status).load();

Calling a javascript function while coding in PHP [duplicate]

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?

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

Categories