...
$elem is an array, so printing it in a string like that would produce "Array."
You have defined no constant i.
$elem has no index i.
.........
Your problem, however, is that you are trying to use JavaScript (NOT java) and PHP at the same time. PHP happens first, on your server. JavaScript happens much later, on the user's machine - it has no idea PHP even exists.