I want to put a javascript code in PHP variable exactly like this:
$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>';
I give me this error:
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
Whats wrong on the code ?
Thanks in Advance
This is your 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>
This is when inside a PHP variable:
$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>';
I guess you have to escape the ' try this:
$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>';
I hope the ' are all in their places now :)
Try this:
$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>";