0

Script I'm using:

if(isset($_GET['cap']) && isset($captions[$_GET['cap']])){
    $cap = $captions[$_GET['cap']];
}

if(isset($cap)){

}

How can I insert the $cap inside a JavaScript parameter?

Example:

tracks: [{
    file: 'http://mylink.com/caption.srt',
    label: 'English',
    kind: 'captions',
    'default': true
}],
5
  • Like this - cap: '<?php echo $cap;?>' Commented Oct 6, 2016 at 3:45
  • 5
    Possible duplicate of How to assign Php variable value to Javascript variable? Commented Oct 6, 2016 at 3:46
  • So, Can I simply insert this instead of link in "file:"? Commented Oct 6, 2016 at 3:47
  • Is your JavaScript inside the php file or whether they are separate? Commented Oct 6, 2016 at 3:57
  • @Rohith Inside! Commented Oct 6, 2016 at 4:08

3 Answers 3

1

Do like this:

<?php

if(isset($_GET['cap']) && isset($captions[$_GET['cap']])){
    $cap = $captions[$_GET['cap']];
?>
<script>
   tracks: [{
      file:<?php echo $cap; ?>,
      label: 'English',
      kind: 'captions',
     'default': true
}],

 </script>
<?php
}
?>

You can combine JS and PHP, by keeping JS inside a PHP if statement, it shall work as you expect

OR

You may try this:

 <?php

if(isset($_GET['cap']) && isset($captions[$_GET['cap']])){
    $cap = $captions[$_GET['cap']];

 echo"<script>tracks:file:".$cap.",label: 'English',kind: 'captions','default':true}],</script>"


}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Value "tracks" is inside an echo, I think will not work.
It's working! I did some changes in your script and it's perfect right now, thanks! It wasn't working because I forgot the ".$cap." syntax to call the php function.
0

This would assign the value into file

tracks: [{
    file: '<b><?= $cap ?></b>',
    label: 'English',
    kind: 'captions',
    'default': true
}],

Comments

0

In your script

 <script>

   var cap = '<?php echo $cap; ?>'
   tracks: [{
      file:cap,
      label: 'English',
      kind: 'captions',
     'default': true
}],

 </script>

Now cap is can be use in your scripts.

3 Comments

How can I insert the $cap onto "file:" instead of link?
Once you share the cap variable in your file cap has value of $cap .You can use it your script!
make sure the js and php scipts are loaded in the same file!

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.