Maikel Richard
Maikel Richard

Reputation: 23

Integration PHP + Javascript

I have a problem with integrations in the php code in javascript within single quotes

I need to put the variable $video that will get the id of a YT video, instead of the id of the video **VARIABLEVIDEOHER**E, but I tried all ways to put single and double quotes did not work to integrate more, someone could give me a light

<?php $video = 'BYN-DEM7Mzw';
$desc = '<script type="text/javascript">
function youtubeFeedCallback( data ){
document.writeln( data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, "<br/>" ) + "<br/>" ); }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/VARIAVELVIDEOAQUI?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback"></script>';
echo $desc; ?>

Upvotes: 0

Views: 133

Answers (2)

Mahn
Mahn

Reputation: 16585

Is this what you mean?

<?php $video = 'BYN-DEM7Mzw';
$desc = '<script type="text/javascript">
function youtubeFeedCallback( data ){
document.writeln( data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, "<br/>" ) + "<br/>" ); }
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/'.$video.'?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback"></script>';
echo $desc; ?>

It seems you are just trying to insert the variable in a string, not really a javascript problem. As a side note, copying code from the internet without actually understanding what it does is genuinely bad, and generally leads to unmaintainable spaghetti code that breaks easily, but that's a different issue.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798794

Use json_encode() to turn the string into a JavaScript literal, and then add it to the rest of the string.

var foo = "bar" + <?php echo json_encode('quux'); ?>;

Upvotes: 1

Related Questions