inTOWN
inTOWN

Reputation: 1010

Pull jquery string from php value

I am using Twitterfetcher of Jason Mayes which works perfectly, except that I need to add the twitter_id coming from a PHP value. Probably you all out there have that answer in a second:

This is the jQuery Code, the number is de Twitter ID. If I fill it out as a string, it works

twitterFetcher.fetch('409855577100005376', 'twitter-feed', 3, true, false, true, '', false, handleTweets3); 

But I need to put in de Twitter ID dynamically, with the value coming from php.

<?php 
$twitter_id= '409855577100005376';
?>

`<script type="text/javascript"> jQuery(document).ready(function($) {    
twitterFetcher.fetch($twitter_id, 'twitter-feed', 3, true, false, true, '', false, handleTweets3);
</script>

What would be the best way to do this?

Upvotes: 0

Views: 186

Answers (3)

dSquared
dSquared

Reputation: 9825

The other answers work well enough if you plan to have the JavaScript inline in the </head> of each page; however if you need to use the var in a separate file the following might be preferable.

In the </head> section of the PHP page you can include a namespaced var, assign the value via PHP and then you can recall it anywhere in your JavaScript File like so:

PHP File

<head>
    ...
    <script type="text/javascript">
        window.NAMESPACE = {};
        NAMESPACE.twitter_id = "<?php echo '1234567890'; ?>";
    </script>
    ...
</head>

JS FILE

...
twitterFetcher.fetch(
    NAMESPACE.twitter_id,
    'twitter-feed',
    3,
    true,
    false,
    true,
    '',
    false,
    handleTweets3
);
...

I hope this helps!

Upvotes: 1

Pwner
Pwner

Reputation: 771

This should work, PHP is processed before JS

<?php 
$twitter_id= '409855577100005376';
?>

<script type="text/javascript"> jQuery(document).ready(function($) {    
twitterFetcher.fetch('<?php echo $twitter_id; ?>', 'twitter-feed', 3, true, false, true, '', false, handleTweets3);
</script>

Upvotes: 1

Goikiu
Goikiu

Reputation: 560

this line

 twitterFetcher.fetch($twitter_id

you need to print the var...

 twitterFetcher.fetch(<?php print $twitter_id;?>

Upvotes: 2

Related Questions