Larry Martell
[PHP] Including JS code that uses PHP
September 05, 2017 11:10PM
I have some javascript code that has PHP in it like this:

var CLIENT_ID='<?php echo $CLIENT_ID; ?>'

If I put the JS code directly in a php file with:

<script type="text/javascript">
.
.
.
</script>

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
this:

<script src="login.js" type="text/javascript">

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?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Aziz Saleh
Re: [PHP] Including JS code that uses PHP
September 05, 2017 11:10PM
On Tue, Sep 5, 2017 at 4:58 PM, Larry Martell <[email protected]>
wrote:

> I have some javascript code that has PHP in it like this:
>
> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
>
> If I put the JS code directly in a php file with:
>
> <script type="text/javascript">
> .
> .
> .
> </script>
>
> 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
> this:
>
> <script src="login.js" type="text/javascript">
>
> 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?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
It is because .js files are not parsed by PHP, not by default. An option
would be to just src="login.js.php" and then in that file output the client
id + js contenttype headers.
Larry Martell
Re: [PHP] Including JS code that uses PHP
September 05, 2017 11:20PM
On Tue, Sep 5, 2017 at 5:03 PM, Aziz Saleh <[email protected]> wrote:
>
>
> On Tue, Sep 5, 2017 at 4:58 PM, Larry Martell <[email protected]>
> wrote:
>>
>> I have some javascript code that has PHP in it like this:
>>
>> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
>>
>> If I put the JS code directly in a php file with:
>>
>> <script type="text/javascript">
>> .
>> .
>> .
>> </script>
>>
>> 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
>> this:
>>
>> <script src="login.js" type="text/javascript">
>>
>> 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?

> It is because .js files are not parsed by PHP, not by default. An option
> would be to just src="login.js.php" and then in that file output the client
> id + js contenttype headers.

With that the PHP is being evaluated but it's resolving to an empty
string and not what $CLIENT_ID has in the PHP world.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Tolga
Re: [PHP] Including JS code that uses PHP
September 05, 2017 11:30PM
On 06-09-2017 00:10, Larry Martell wrote:
> On Tue, Sep 5, 2017 at 5:03 PM, Aziz Saleh <[email protected]> wrote:
>>
>> On Tue, Sep 5, 2017 at 4:58 PM, Larry Martell <[email protected]>
>> wrote:
>>> I have some javascript code that has PHP in it like this:
>>>
>>> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
>>>
>>> If I put the JS code directly in a php file with:
>>>
>>> <script type="text/javascript">
>>> .
>>> .
>>> .
>>> </script>
>>>
>>> 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
>>> this:
>>>
>>> <script src="login.js" type="text/javascript">
>>>
>>> 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?
>> It is because .js files are not parsed by PHP, not by default. An option
>> would be to just src="login.js.php" and then in that file output the client
>> id + js contenttype headers.
> With that the PHP is being evaluated but it's resolving to an empty
> string and not what $CLIENT_ID has in the PHP world.
>
how about like this?

<script>var CLIENT_ID='<?=$CLIENT_ID?>';</script>
<script src="login.js"></script>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] Including JS code that uses PHP
September 05, 2017 11:40PM
On 5 September 2017 22:10:09 BST, Larry Martell <[email protected]> wrote:
>On Tue, Sep 5, 2017 at 5:03 PM, Aziz Saleh <[email protected]> wrote:
>>
>>
>> On Tue, Sep 5, 2017 at 4:58 PM, Larry Martell
><[email protected]>
>> wrote:
>>>
>>> I have some javascript code that has PHP in it like this:
>>>
>>> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
>>>
>>> If I put the JS code directly in a php file with:
>>>
>>> <script type="text/javascript">
>>> .
>>> .
>>> .
>>> </script>
>>>
>>> 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
>>> this:
>>>
>>> <script src="login.js" type="text/javascript">
>>>
>>> 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?
>
>> It is because .js files are not parsed by PHP, not by default. An
>option
>> would be to just src="login.js.php" and then in that file output the
>client
>> id + js contenttype headers.
>
>With that the PHP is being evaluated but it's resolving to an empty
>string and not what $CLIENT_ID has in the PHP world.

If it's doing that, it suggests you have an error. What does your error log say?
Thanks,
Ash

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP] Re: Including JS code that uses PHP
September 05, 2017 11:40PM
On 05.09.2017 at 22:58, Larry Martell wrote:

> I have some javascript code that has PHP in it like this:
>
> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
>
> If I put the JS code directly in a php file with:
>
> <script type="text/javascript">
> .
> .
> .
> </script>
>
> 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
> this:
>
> <script src="login.js" type="text/javascript">
>
> 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
to JavaScript instead. A very simplistic way might be:

PHP file:

<?php $serverData = ['client_id' => CLIENT_ID']?>
<script>var serverData = <?=json_encode($serverData)?></script>
<script src="login.js">

JavaScript file (login.js):

alert(serverData.CLIENT_ID);

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Martell
[PHP] Re: Including JS code that uses PHP
September 06, 2017 12:40AM
On Tue, Sep 5, 2017 at 5:35 PM, Christoph M. Becker <[email protected]> wrote:
> On 05.09.2017 at 22:58, Larry Martell wrote:
>
>> I have some javascript code that has PHP in it like this:
>>
>> var CLIENT_ID='<?php echo $CLIENT_ID; ?>'
>>
>> If I put the JS code directly in a php file with:
>>
>> <script type="text/javascript">
>> .
>> .
>> .
>> </script>
>>
>> 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
>> this:
>>
>> <script src="login.js" type="text/javascript">
>>
>> 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
> to JavaScript instead. A very simplistic way might be:
>
> PHP file:
>
> <?php $serverData = ['client_id' => CLIENT_ID']?>
> <script>var serverData = <?=json_encode($serverData)?></script>
> <script src="login.js">
>
> JavaScript file (login.js):
>
> alert(serverData.CLIENT_ID);

Thanks - I did a variation of that and it worked.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login