-4

I've spent a big portion of today solving this. But couldn't.

Basically, I want to embed an Youtube video in a PHP page. The videoid variable is defined in the PHP page but I cannot make the JS display that variable.

<script>
  // Load the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
      height: '460',
      width: '760',
      videoId: '<? echo $var;>'
    });
  }
</script>

The $var variable is present globally within the PHP file but it doesn't work. I also declared a variable within the JS, with no success. What am I doing wrong?

Edit:

Since it's marked as duplicate, let me explain. I'm not having an issue understanding how to output the PHP variable, it just doesn't work, no matter how I try.

Ivan
  • 34,531
  • 8
  • 55
  • 100
vexx
  • 27
  • 6
  • 1
    `var videoid = ;` - or use [ajax](https://stackoverflow.com/a/13641184/5827005). – GrumpyCrouton Aug 14 '17 at 13:35
  • 1
    Why don't you use the PHP and set it to a cookie which you can then access easily in the JS portion of your code – Oluwaseye Aug 14 '17 at 13:38
  • var videoid = ; doesn't work, when I look in the html source, the file writes the code, not the output. – vexx Aug 14 '17 at 13:43
  • Firstly - do you have PHP short tags enabled? Otherwise you'll probably want either `` or `= $var; ?>` (5.6+). Secondly, what value is *actually* in `$var` at that moment in time? Try putting in something like `alert('= $var; ?>');` for debugging. – CD001 Aug 14 '17 at 13:48
  • Ya, short tags are enabled. If I write alert('= $var; ?>'); it outputs the code, not the value. Not sure what's going on. I did a test output outside the – vexx Aug 14 '17 at 13:53
  • The actual value is the video id taken from the database. It outputs correctly if I echo it in the same php file. – vexx Aug 14 '17 at 13:54
  • Is your javascript code declared before the php variable? If so, there's your problem. – GrumpyCrouton Aug 14 '17 at 13:59
  • No, it's after the declaration. THe site is made with Smarty and the code this code is added in the .tpl file. The variable is declared in a php file which is assigned to that tpl, making it global for that tpl. – vexx Aug 14 '17 at 14:04
  • You know you have to use `{php}` tag in smarty if you want to execute php inside the template right? try `videoId: '{php} echo $var; {/php}'` or it might just work if you write `videoId: '{$var}'` – xander Aug 14 '17 at 14:07
  • Ya I know, I tried both, {php} and videoId: '{$var}'. It outputs $var, not the actual value. If I write alert('{php} echo $var; {/php}'); the popup says "{php} echo $var; {/php}" – vexx Aug 14 '17 at 14:13
  • Well I guess you have put the script element inside of a smarty `{literal}` tag, that would explain why nothing inside is evaluated, no php, no nothing it's just a literal! :D – xander Aug 14 '17 at 14:17
  • The whole script is wrapped in {literal}, otherwise the page will give an error (there are curly brackets in the function) :) – vexx Aug 14 '17 at 14:19
  • Then I removed the {literal} and added just {ldelim} and {rdelim} instead of the curly brackets. This didn't work either :( – vexx Aug 14 '17 at 14:20
  • Yes but you didn't say any of that in your question. Anyway to fix it you have to put the php code outside of the literal, so you can either escape all your `{}` within the script or break the literal for your echo, not pretty but should work: `videoId: '{/literal}{$var}{literal}'` – xander Aug 14 '17 at 14:21
  • wow, that actually worked..wth...You save me a ton. However, I don't understand, I removed the literal and just used {ldelim} and {rdelim} ...why did it took the whole thing literal? – vexx Aug 14 '17 at 14:29
  • It should work with `{ldelim}` etc without the `{literal}`, but you still have to use {$var} or `{php} ...` and not ` – xander Aug 14 '17 at 14:31

1 Answers1

0

With Smarty as your template engine you usually have to put your script code inside a {literal} tag, so to execute actualy php code inside your literal tag you have to put the php code outside of the literal, so you can either escape all your { } within the script or break the literal for your echo, not pretty but should work: videoId: '{/literal}{$var}{literal}'

xander
  • 1,780
  • 9
  • 16