Jump to content

PHP Inside Javascript?


XRS

Recommended Posts

After reading some threads around the net I can't find a situable solutions for this.

In my script I want to use a javascript code ( external source from another website ) and there's a JS variable with an ID ( the user ID in the other website).

 

In my script I have the ID stored in the DB and I want to insert it in the javascript by the ID listed in the DB ( that can be changed, so there's the PHP needed )

 

The JS code:

<script>
var siteID = 11111;

all rest of code
</script>

 

How can I change the value of siteID in JS for the one I want? Lets say I would like something like:

 

var siteID = <?php GetOtherSiteID(); ?>;

 

 

This seems no to be possible. There's any sugestion?

Link to comment
https://forums.phpfreaks.com/topic/240201-php-inside-javascript/
Share on other sites

After reading some threads around the net I can't find a situable solutions for this.

In my script I want to use a javascript code ( external source from another website ) and there's a JS variable with an ID ( the user ID in the other website).

 

In my script I have the ID stored in the DB and I want to insert it in the javascript by the ID listed in the DB ( that can be changed, so there's the PHP needed )

 

The JS code:

<script>
var siteID = 11111;

all rest of code
</script>

 

How can I change the value of siteID in JS for the one I want? Lets say I would like something like:

 

var siteID = <?php GetOtherSiteID(); ?>;

 

 

This seems no to be possible. There's any sugestion?

 

Does the php have line breaks in it?

http://www.the-art-of-web.com/php/javascript-escape/

<script>
var siteID = <?php echo GetOtherSiteID(); ?>;

all rest of code
</script>

 

//EDIT

 

If your javascript is stored in an external file, which I shall assume it is not since you used script tags, save it as a php file (.php) and add a javascript content-type header ( header('content-type: text/javascript'); ) and you can run php in the javascript file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.