Although you have already got this working, I'm gonna post my opinion anyway for anyone else who is looking at this.
Whenever I use javascript inside my PHP i do it like this:
<?php
// some PHP code
$if ($blah == "hello") {
?>
<script>
// My javascript
alert("Hello. This is Matt's Javascript.")
</script>
<?php
}
?>
It doesnt hav 2 be in an if statement obviously but i put it there cos i felt like it 🙂
Its easier to do it outside the PHP simply coz u dont hav 2 do any extra \'s 🙂
for example, if this is a line of javascript:
alert("Matt says \"hi there\".");
then in php it would b like this:
echo "alert(\\"Matt says \\\\\\"hi there\\\\\\".\\");";
And that looks confuzing (i proly wrote it wrong anyways 🙂)
Hope this helps - its just my opinion 🙂