-2

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?

Hendry
  • 87
  • 1
  • 9

2 Answers2

1

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>
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
1

You can't really parse PHP values in a JavaScript file. If you want the value either echo it in place or in a script tag on the PHP page as a variable so you can access it.

<div id="div-test"><?php echo $money;?><div>

You could then get the text value of #div-test in your JS file.

Or:

<script type="text/javascript">
    var money = <?php echo $money;?>;
</script>

Then you could access money as a variable in your test.js file

Taintedmedialtd
  • 856
  • 5
  • 13