George
George

Reputation: 2110

passing php function to jquery

Consider the following Jquery external script snippet for creating youtube iframes in modal windows:

$(function() {

    $(document).ready(function() {  

    var myPhpVa = '<?php echo embeddable($ytid);?>';

    $('a[name="modal"]').click(function(e) {

        $('<iframe width="560" height="315" src=myPhpVa frameborder="0" allowfullscreen>').appendTo('#dcontent');

    }

});

For some reason I get object not found. The PHP function is declared above this javascript, the declaration is:

function embeddable($ytid){
        return $embeddable = 'http://www.youtube.com/embed/'.$ytid;
    }

Any help for passing this PHP variable is greatly appreciated.

Upvotes: 1

Views: 119

Answers (3)

ghostJago
ghostJago

Reputation: 3434

Looks like you are not breaking out of the string to concat the myPhpVa into the js literal. Try this:

$('<iframe width="560" height="315" src="' +  myPhpVa + '" frameborder="0" allowfullscreen>').appendTo('#dcontent'); 

If that doesn't work, alert the value of myPhpVa to see if its what you expect.

Upvotes: 1

Mathieu
Mathieu

Reputation: 3083

I think the syntax of your code where you append the iframe to your html is incorrect.

    $('<iframe width="560" height="315" src="' + myPhpVa + '" frameborder="0" allowfullscreen>').appendTo('#dcontent');

Upvotes: 2

hsz
hsz

Reputation: 152216

First of all - assigning returned value is useless. Remove it:

function embeddable($ytid){
    return 'http://www.youtube.com/embed/'.$ytid;
}

The same thing about $(document).ready(function() { and $(function() { - they are equal and doubled - remove one of them.

After you assign myPhpVa variable - write

alert(myPhpVa);

and check what it shows you.

Upvotes: 3

Related Questions