I got really confused when i tried to transfer variables from php to js. everything is fine when i try to get value using an id for example:
var name = $
Not quite. Encoding as JSON will ensure that it is a valid JavaScript literal.
var num =