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
}],

3 Answers3

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


}
?>
Rohith K N
  • 845
  • 6
  • 17
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.

Mohamed Nizar
  • 780
  • 9
  • 30
0

This would assign the value into file

tracks: [{
    file: '<b><?= $cap ?></b>',
    label: 'English',
    kind: 'captions',
    'default': true
}],
user2182349
  • 9,569
  • 3
  • 29
  • 41