1

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.

1
  • You don't need to both use $(document).ready(function(){}) and $(function(){}). They are both same Commented Oct 26, 2011 at 9:03

3 Answers 3

3

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.

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

7 Comments

the alert gives <?php echo embeddable() ?>
Ok - what is the extension of your file ? Is it .html or .php ? Are you running this page from PHP server or directly from your computer without any Apache server ?
i call this PHP from JS file and I have apache server running
Ok, so we have a solution. You cannot do that, because .js files are not parsed with PHP. If you want to do things like this, you have place this part of JS script into .php file and let server to execute PHP content. Then it will work.
Ok, could you give some sample code this js file is called modal.js php should echo the whole script?
|
2

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');

Comments

1

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.

Comments

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.