-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?

1
  • Use php tags with echo in document.getElementById("div-test").innerHTML = $money; Commented Mar 1, 2017 at 13:33

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Just the simple answer i was looking for, thank you kindly!
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.