Brilliant! Thanks Anthony - that was the problem. I thought I was doing something completely wrong as combining javascript and php always gets me confused:)
In the future, first, get the javascript working by itself. If this means hardcoding certain values, fine.
Once you have that done, then, you can worry about using php to dynamically create the value. The benefit here is, your goal with php is reduced to “make php generate some text that looks just like the working javascript”.
You would have noticed right away that the working javascript