Answers for "how set variable is php at js"

PHP
10

using js variable in php

<script type="text/javascript">
var abc= 'this is text';
<?php $abc = "<script>document.write(abc)</script>"?>   
</script>
<?php echo $abc;?>
Posted by: Guest on June-10-2020
0

use php var in js

// (A) CONVERT ARRAY TO STRING
$data = ["Apple", "Banana", "Cherry"];
$encoded = json_encode($data);
?>
 
<!-- (B) THIS IS JAVASCRIPT -->
<script>
// (B1) JSON ENCODED STRING
var encoded = '<?=$encoded?>';
console.log(encoded); // STRING
 
// (B2) JSON PARSE (DECODE)
var data = JSON.parse(encoded);
console.log(data); // ARRAY
Posted by: Guest on February-25-2022

Browse Popular Code Answers by Language