This has nothing to do with PHP: You're outputting a string. That string happens to be JavaScript that happens to be interpreted by the browser. You need to debug that JavaScript.
For example, this line:
$myWin=window.open(...
will result in
=window.open(...
because $myWin is interpreted as a variable by PHP (unless the $myWin variable was set in PHP - it's not entirely clear from your code whether it is or not, but my guess it is not.)
The solution - unless, as I said myWin is set - would be removing the $ from the variable name to make it a normal JS variable.
Always look at the end result in your browser. And, please please, describe the problem in more detail than "doesn't work"!