Difference between revisions of "Passing variables from php to javascript"

From MyWiki
Jump to: navigation, search
(Created page with "https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript<br>")
 
 
Line 1: Line 1:
 
https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript<br>
 
https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript<br>
 +
<source lang="php">
 +
<!-- snip -->
 +
<div id="dom-target" style="display: none;">
 +
    <?php
 +
        $output = "42"; //Again, do some operation, get the output.
 +
        echo htmlspecialchars($output); /* You have to escape because the result
 +
                                          will not be valid HTML otherwise. */
 +
    ?>
 +
</div>
 +
<script>
 +
    var div = document.getElementById("dom-target");
 +
    var myData = div.textContent;
 +
</script>
 +
<!-- snip -->
 +
</source>

Latest revision as of 10:41, 20 January 2019

https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript

<!-- snip -->
<div id="dom-target" style="display: none;">
    <?php 
        $output = "42"; //Again, do some operation, get the output.
        echo htmlspecialchars($output); /* You have to escape because the result
                                           will not be valid HTML otherwise. */
    ?>
</div>
<script>
    var div = document.getElementById("dom-target");
    var myData = div.textContent;
</script>
<!-- snip -->