0

I'm trying to insert php variable in a javascript code but unable to get the result. Code is:

<?php $twitterusername = get_option_tree( 'twitter_name', '', 'true' );?>

<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=$twitterusername&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>

Actually my text editor is not marking $twitterusename inside the script as valid php code. Please help me out.

4 Answers 4

1

Try this:

<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?php echo $twitterusername;?>settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>

As it's a PHP variable, you need to echo it out with PHP, not javascript.

So you need to replace

$twitterusername

with

<?php echo $twitterusername; ?>

Even though it's inside the <script> tags, it's going to echo out whatever $twitterusername is as long as it's in PHP tags.

If your server supports shortcode, you could use

<?=$twitterusername;?>

making it slightly shorter.

<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?=$twitterusername;?>settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
Sign up to request clarification or add additional context in comments.

Comments

1

It is not valid php code, you need to mark it as so using <?php echo $twitterusername; ?>.

Your final code would look like:

<?php $twitterusername = get_option_tree( 'twitter_name', '', 'true' );?>

<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?php echo $twitterusername; ?>&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>

Comments

0

You need to use php tags (<? ?>) to insert variable.

<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?=$twitterusername?>&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>

Comments

-1

It's because you are using your PHP variable outside PHP tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.