pass a php variable to javascript ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

pass a php variable to javascript ?

Post by aneuryzma »

How can I pass a php variable to javascript variable ?

Code: Select all

<?php
$myVariable = "hello";
?>
 
 
<script type="text/javascript">
 
var myVariable = ..... $myVariable;
 
</script>
thanks
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: pass a php variable to javascript ?

Post by jazz090 »

this is easy:

Code: Select all

<?php
$var = "hello";
?>

Code: Select all

<script type="text/javascript">
var newvar = "<?php echo $var; ?>";
</script>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: pass a php variable to javascript ?

Post by califdon »

Code: Select all

<?php
$myVariable = "hello";
echo "<script type='text/javascript'>";
echo "var myVariable = '$myVariable'";
echo "</script>";
?>
Post Reply