PHP变量中的Javascript代码?

时间:2015-02-03 18:57:12

标签: javascript php

我想在PHP变量中放入一个javascript代码:

$ch = '<script> var w = window.innerWidth; var h = window.innerHeight; var id = "'. $row['ch_id'] .'" ; document.write(<iframe width="'+w+'" height="'+h+'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="true" src="http://www.exemple/embed.php?width='+w+'&height='+h+'&channel='+id+'&autoplay=true"></iframe>); </script>';

我给了我这个错误:

Notice: Use of undefined constant w - assumed 'w' in /test.php on line 9

Notice: Use of undefined constant h - assumed 'h' in /test.php on line 9

Notice: Use of undefined constant w - assumed 'w' in /test.php on line 9

Notice: Use of undefined constant h - assumed 'h' in /test.php on line 9

Notice: Use of undefined constant id - assumed 'id' in /test.php on line 9

代码有什么问题?

先谢谢

3 个答案:

答案 0 :(得分:3)

这是您的Javascript:

<script>
var w = window.innerWidth; 
var h = window.innerHeight; 
var id = 23;   // For example
document.write('<iframe width= height="'+h+'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="true" src="http://www.exemple/embed.php?width="'+w+'"&height="'+h+'"&channel="'+id+'"&autoplay=true"></iframe>');
</script>


这是在PHP变量中的时候:

$ch = '<script>
       var w = window.innerWidth; 
       var h = window.innerHeight; 
       var id = "'. $row['ch_id'] .'"; 
       document.write(\'<iframe width= height="\'+h+\'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="true" src="http://www.exemple/embed.php?width="\'+w+\'"&height="\'+h+\'"&channel="\'+id+\'"&autoplay=true"></iframe>\');
       </script>';


演示:
http://3v4l.org/fH0Vi

答案 1 :(得分:0)

我想你必须逃避&#39;试试这个:

$ch = '<script> var w = window.innerWidth; var h = window.innerHeight; var id = "'. $row['ch_id'] .'" ; document.write(\'<iframe width="\'+w+\'" height="\'+h+\'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="true" src="http://www.exemple/embed.php?width=\'+w+\'&height=\'+h+\'&channel='.$row['chd_id'].'&autoplay=true"></iframe>\'); </script>';

我希望&#39;现在都在他们的位置:)

答案 2 :(得分:-1)

试试这个:

$ch = "<script> var w = window.innerWidth; var h = window.innerHeight; var id = '". $row['ch_id'] ."' ; document.write(<iframe width="\'+w+\'" height="\'+h+\'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="true" src= \"http://www.exemple/embed.php?width='".$w."'&height='".$h."'&channel='".$row['chd_id']."'&autoplay=true\"></iframe>); </script>";