PHP Question

How to set PHP tag $value into a Javascript parameter?

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

Answer

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


}
?>