Search code examples
phpjavascriptpostget

Send PHP variable to JavaScript function


I have a php file that generates a variable and I would like the variable to be put into a JavaScript function which is called by onclick on the main page. Is this possible to send from PHP to JavaScript?


Solution

  • You can do the following:

    <script type='text/javascript'>
        document.body.onclick(function(){
            var myVariable = <?php echo(json_encode($myVariable)); ?>;
        };
    </script>