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

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

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 put javascript variable in php array [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 4 years ago.
I need to add JavaScript variable to PHP array, my code is below and var date1 is JavaScript variable.
Code
$date1="2018-08-02";
$date=explode("-",$date1);
My JavaSript section is here
var date1=new Date(<?=$date[0].",".$date[1].",".$date[2]?>);
date1 place to index of array x
$incoming[$i] = array("x" =>+date1 , "y" => $total_inc);
$outgoing[$i] = array("x" => +date1, "y" => $total_out);
In this situation the value is return 0.
You can't. JS code is executed after PHP finished executing own code. You can pass data from js to php by ajax or by setting a cookie.

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

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