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
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/

Link to comment
Share on other sites

No, there's no line breaks. I'ts a simple function that return a number from the DB.

In this case, adfly API tool javascript code.. It have a ID you have to use, but another user have another ID.

 

That ID is defined in the DB instead of edit it manually.

Any ideas?

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.