Hendry Hendry -4 years ago 136
PHP Question

Use php var in js file

I've been looking, but found nothing similar to my case.

I want my php variable $money , to pass onto my javascript file.

Example :

** FILE index.php **
<?php $money = 15; ?>

<div id="div-test"> Foo foo<div>

<script src="test.js"></script>


** FILE test.js **
document.getElementById("div-test").innerHTML = $money;


How would i make this possible?

Answer Source

In php tag

<?php $money = 15; ?>


<script>
var val = <?php echo $money; ?>

<div id="div-test"> Foo foo<div>

<script src="test.js"></script>


** FILE test.js ** 
document.getElementById("div-test").innerHTML = val;
</script>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download