Q:

send php variable to javascript

<script type="text/javascript">
   var php_var = "<?php echo $php_var; ?>";
</script>
6
<?php
$php_variable = 'string'; //Define our PHP variable. You can of course get the value of this variable however you need.
?>			
        <script> js_variable_name = "<?php echo $php_variable; ?>";</script>
1
<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        How to pass variables and data 
        from PHP to JavaScript? 
    </title> 
</head> 
  
<body style="text-align:center;"> 
      
    <h1 style="color:green;">GeeksforGeeks</h1> 
      
    <h4> 
        How to pass variables and data 
        from PHP to JavaScript? 
    </h4> 
      
    <?php 
        $name = "Hello World"; 
    ?> 
  
    <script type="text/javascript"> 
        var x = "<?php echo"$name"?>"; 
        document.write(x); 
    </script> 
</body> 
  
<html> 
0
<script type="text/javascript">
// boolean outputs "" if false, "1" if true
var bool = "<?php echo $bool ?>"; 

// numeric value, both with and without quotes
var num = <?php echo $num ?>; // 7
var str_num = "<?php echo $num ?>"; // "7" (a string)

var str = "<?php echo $str ?>"; // "A string here"
</script>
0

New to Communities?

Join the community