im not sure what you are doing will work...you can't have javascript execute php...
you can have javascript ask the server to execute php by submitting anther request... Perhaps by doing a document.location, or by loading into an image from the server, or by refreshing the page, or in some way having the server run your php for you and send it back to the browser... once javascript gets a change to execute... any php it writes won't be executed.... it will just look like text in the page
but probably the most insecure thing i can think of is to have javascript write some php code... and then submit it to the server for php to run without question... if you do this than you don't need to be using php in the first place.. and someone at php should come to your house and confiscate your computer for the good of the industry...
however, to answer your question
you get a literal \ by doing \
you get a liteal \ by doing \\
so you can just keep \\\ing all day and each successive read of it will in effect get rid of half the \ characters...
maybe that can get you where you want to be with your code...
or did i totally misunderstand what you wanted