[PHP] Re: Including JS code that uses PHP
- Date: Tue, 5 Sep 2017 23:35:37 +0200
- From: "Christoph M. Becker" <cmbecker69@xxxxxx>
- Subject: [PHP] Re: Including JS code that uses PHP
On 05.09.2017 at 22:58, Larry Martell wrote:
> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
> If I put the JS code directly in a php file with:
> Then at run time CLIENT_ID has the value I want. I want to use the
> same JS code in many php files. But if I include the JS code like
> Then that php code does not get run and CLIENT_ID has the literal
> string '<?php echo $CLIENT_ID; ?>'
> Is there a way I can reuse my JS file and still have embedded PHP in it?
There might be, but consider to simply pass the required values as data
<?php $serverData = ['client_id' => CLIENT_ID']?>
<script>var serverData = <?=json_encode($serverData)?></script>
Christoph M. Becker
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php